-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvScheduler.hpp
73 lines (73 loc) · 2.28 KB
/
ConvScheduler.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
#include "Scheduler.hpp"
class ConvScheduler: private Scheduler
{
private:
multiset<pair<ll, ll>> J;
public:
using Scheduler::fft_test;
using Scheduler::sumset_test;
using Scheduler::subsetsum_test;
using Scheduler::maxminskewed_test;
ConvScheduler()
{
J = multiset<pair<ll, ll>>();
}
void insert(ll due, ll process)
{
J.insert({ due, process });
}
void remove(ll due, ll process)
{
J.erase({ due, process });
}
void clear()
{
J.clear();
}
ll run()
{
vector<pair<ll, multiset<ll>>> X;
ll P = 0LL;
for (auto i = J.begin(); i != J.end(); i++)
{
auto j = i;
if (i == J.begin() || (*i).first != (*(--j)).first) X.push_back({ (*i).first, multiset<ll>() });
X[X.size() - 1LL].second.insert((*i).second);
P += (*i).second;
}
ll dsharp = X.size();
vector<multiset<ll>> S(dsharp);
vector<vector<ll>> M(dsharp, vector<ll>(P + 1LL, 0LL));
for (ll i = 0LL; i < dsharp; i++)
{
S[i] = subsetsum(X[i].second);
for (ll j = 0LL; j <= P; j++)
{
if (j == 0LL) M[i][j] = X[i].first;
else if (S[i].find(j) != S[i].end() && j <= X[i].first) M[i][j] = X[i].first - j;
else M[i][j] = NINF;
}
}
while (dsharp > 1LL)
{
for (ll i = 1LL; i <= dsharp / 2LL; i++)
{
M[2LL * i - 2LL][0LL] = M[2LL * i - 1LL][0LL];
vector<ll> b = M[2LL * i - 2LL], a = M[2LL * i - 1LL];
for (ll j = 0LL; j <= P; j++) a[j] += j;
M[i - 1LL] = maxminskewedconvolution(a, b);
M[i - 1LL].resize(P + 1);
for (ll j = 0LL; j <= P; j++) if ((M[i - 1LL][j] = M[i - 1LL][j] - j) < 0LL) M[i - 1LL][j] = NINF;
}
for (ll i = dsharp / 2LL + 1LL; 2LL * i - 1LL <= dsharp; i++) M[i - 1LL] = M[2LL * i - 2LL];
dsharp = ll(ceil(dsharp / 2.0L));
}
for (ll i = P; i >= 0LL; i--) if (M[0LL][i] >= 0LL) return P - i;
return -1LL;
}
~ConvScheduler()
{
J.clear();
}
};