#include "job.h"
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
struct Job {
int64 u, d, id;
Job(int64 _u, int64 _d, int64 _id) : u(_u), d(_d), id(_id) {}
bool operator < (const Job &rhs) const {
return d * rhs.u < rhs.d * u;
}
};
int64 scheduling_cost(vector<int> p, vector<int> u, vector<int> d) {
int N = p.size();
vector<set<int>> ch(N);
for (int i = 1; i < N; ++i) ch[p[i]].emplace(i);
multiset<Job> jobs;
for (int i = 0; i < N; ++i) jobs.emplace(u[i], d[i], i);
int64 tim = 0, ans = 0;
while (jobs.size()) {
Job job = *begin(jobs);
jobs.erase(begin(jobs));
// cerr << "(" << job.u << ", " << job.d << ", " << job.id << ")\n";
if (u[job.id] != job.u or d[job.id] != job.d) continue;
// cerr << "(" << job.u << ", " << job.d << ", " << job.id << ")\n";
if (p[job.id] == -1) {
ans += job.u * (tim += job.d);
for (int x : ch[job.id]) p[x] = -1;
}
else {
ans -= u[p[job.id]] * job.d;
int64 new_u = u[p[job.id]] + job.u;
int64 new_d = d[p[job.id]] + job.d;
if (ch[p[job.id]].size() <= ch[job.id].size()) {
ch[p[job.id]].erase(job.id);
if (p[p[job.id]] != -1) ch[p[p[job.id]]].erase(p[job.id]), ch[p[p[job.id]]].emplace(job.id);
for (int x : ch[p[job.id]]) ch[job.id].emplace(x), p[x] = job.id;
ch[p[job.id]].clear(), u[p[job.id]] = d[p[job.id]] = -1;
p[job.id] = p[p[job.id]];
jobs.emplace(u[job.id] = new_u, d[job.id] = new_d, job.id);
}
else {
for (int x : ch[job.id]) ch[p[job.id]].emplace(x), p[x] = p[job.id];
ch[job.id].clear(), u[job.id] = d[job.id] = -1;
jobs.emplace(u[p[job.id]] = new_u, d[p[job.id]] = new_d, p[job.id]);
}
}
// for (int i = 0; i < N; ++i) cerr << p[i] << " \n"[i == N-1];
// for (int i = 0; i < N; ++i) cerr << u[i] << " \n"[i == N-1];
// for (int i = 0; i < N; ++i) cerr << d[i] << " \n"[i == N-1];
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
41 ms |
9308 KB |
Output is correct |
6 |
Correct |
100 ms |
18420 KB |
Output is correct |
7 |
Correct |
210 ms |
27216 KB |
Output is correct |
8 |
Correct |
308 ms |
36320 KB |
Output is correct |
9 |
Correct |
267 ms |
36432 KB |
Output is correct |
10 |
Correct |
267 ms |
36512 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
285 ms |
36432 KB |
Output is correct |
13 |
Correct |
171 ms |
36328 KB |
Output is correct |
14 |
Correct |
201 ms |
39508 KB |
Output is correct |
15 |
Correct |
133 ms |
39600 KB |
Output is correct |
16 |
Correct |
164 ms |
39760 KB |
Output is correct |
17 |
Correct |
270 ms |
39500 KB |
Output is correct |
18 |
Correct |
301 ms |
39620 KB |
Output is correct |
19 |
Correct |
128 ms |
39468 KB |
Output is correct |
20 |
Correct |
106 ms |
39508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
166 ms |
36216 KB |
Output is correct |
5 |
Correct |
175 ms |
36328 KB |
Output is correct |
6 |
Correct |
173 ms |
36440 KB |
Output is correct |
7 |
Correct |
163 ms |
36296 KB |
Output is correct |
8 |
Correct |
215 ms |
36448 KB |
Output is correct |
9 |
Correct |
162 ms |
36532 KB |
Output is correct |
10 |
Correct |
163 ms |
36476 KB |
Output is correct |
11 |
Correct |
193 ms |
36364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
6 ms |
2136 KB |
Output is correct |
6 |
Correct |
163 ms |
36436 KB |
Output is correct |
7 |
Correct |
169 ms |
36300 KB |
Output is correct |
8 |
Correct |
161 ms |
36332 KB |
Output is correct |
9 |
Correct |
157 ms |
36184 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
1884 KB |
Output is correct |
13 |
Correct |
7 ms |
2140 KB |
Output is correct |
14 |
Correct |
158 ms |
36296 KB |
Output is correct |
15 |
Correct |
162 ms |
36436 KB |
Output is correct |
16 |
Correct |
166 ms |
36444 KB |
Output is correct |
17 |
Correct |
181 ms |
36544 KB |
Output is correct |
18 |
Correct |
170 ms |
36396 KB |
Output is correct |
19 |
Correct |
160 ms |
36324 KB |
Output is correct |
20 |
Correct |
168 ms |
36440 KB |
Output is correct |
21 |
Correct |
161 ms |
36432 KB |
Output is correct |
22 |
Correct |
175 ms |
36432 KB |
Output is correct |
23 |
Correct |
173 ms |
36340 KB |
Output is correct |
24 |
Correct |
196 ms |
36332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
339 ms |
36444 KB |
Output is correct |
3 |
Correct |
365 ms |
36436 KB |
Output is correct |
4 |
Correct |
325 ms |
36444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
41 ms |
9308 KB |
Output is correct |
6 |
Correct |
100 ms |
18420 KB |
Output is correct |
7 |
Correct |
210 ms |
27216 KB |
Output is correct |
8 |
Correct |
308 ms |
36320 KB |
Output is correct |
9 |
Correct |
267 ms |
36432 KB |
Output is correct |
10 |
Correct |
267 ms |
36512 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
285 ms |
36432 KB |
Output is correct |
13 |
Correct |
171 ms |
36328 KB |
Output is correct |
14 |
Correct |
201 ms |
39508 KB |
Output is correct |
15 |
Correct |
133 ms |
39600 KB |
Output is correct |
16 |
Correct |
164 ms |
39760 KB |
Output is correct |
17 |
Correct |
270 ms |
39500 KB |
Output is correct |
18 |
Correct |
301 ms |
39620 KB |
Output is correct |
19 |
Correct |
128 ms |
39468 KB |
Output is correct |
20 |
Correct |
106 ms |
39508 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
166 ms |
36216 KB |
Output is correct |
25 |
Correct |
175 ms |
36328 KB |
Output is correct |
26 |
Correct |
173 ms |
36440 KB |
Output is correct |
27 |
Correct |
163 ms |
36296 KB |
Output is correct |
28 |
Correct |
215 ms |
36448 KB |
Output is correct |
29 |
Correct |
162 ms |
36532 KB |
Output is correct |
30 |
Correct |
163 ms |
36476 KB |
Output is correct |
31 |
Correct |
193 ms |
36364 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
6 ms |
2136 KB |
Output is correct |
37 |
Correct |
163 ms |
36436 KB |
Output is correct |
38 |
Correct |
169 ms |
36300 KB |
Output is correct |
39 |
Correct |
161 ms |
36332 KB |
Output is correct |
40 |
Correct |
157 ms |
36184 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
5 ms |
1884 KB |
Output is correct |
44 |
Correct |
7 ms |
2140 KB |
Output is correct |
45 |
Correct |
158 ms |
36296 KB |
Output is correct |
46 |
Correct |
162 ms |
36436 KB |
Output is correct |
47 |
Correct |
166 ms |
36444 KB |
Output is correct |
48 |
Correct |
181 ms |
36544 KB |
Output is correct |
49 |
Correct |
170 ms |
36396 KB |
Output is correct |
50 |
Correct |
160 ms |
36324 KB |
Output is correct |
51 |
Correct |
168 ms |
36440 KB |
Output is correct |
52 |
Correct |
161 ms |
36432 KB |
Output is correct |
53 |
Correct |
175 ms |
36432 KB |
Output is correct |
54 |
Correct |
173 ms |
36340 KB |
Output is correct |
55 |
Correct |
196 ms |
36332 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
339 ms |
36444 KB |
Output is correct |
58 |
Correct |
365 ms |
36436 KB |
Output is correct |
59 |
Correct |
325 ms |
36444 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
0 ms |
348 KB |
Output is correct |
68 |
Correct |
1 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
1 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
1 ms |
432 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
348 KB |
Output is correct |
85 |
Correct |
1 ms |
348 KB |
Output is correct |
86 |
Correct |
0 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
500 KB |
Output is correct |
88 |
Correct |
4 ms |
1372 KB |
Output is correct |
89 |
Correct |
7 ms |
1760 KB |
Output is correct |
90 |
Correct |
8 ms |
2136 KB |
Output is correct |
91 |
Correct |
57 ms |
10132 KB |
Output is correct |
92 |
Correct |
126 ms |
19936 KB |
Output is correct |
93 |
Correct |
344 ms |
39628 KB |
Output is correct |
94 |
Correct |
277 ms |
39372 KB |
Output is correct |
95 |
Correct |
301 ms |
39760 KB |
Output is correct |
96 |
Correct |
269 ms |
39508 KB |
Output is correct |
97 |
Correct |
343 ms |
39460 KB |
Output is correct |
98 |
Correct |
280 ms |
39764 KB |
Output is correct |
99 |
Correct |
278 ms |
39268 KB |
Output is correct |
100 |
Correct |
292 ms |
39320 KB |
Output is correct |
101 |
Correct |
346 ms |
39376 KB |
Output is correct |
102 |
Correct |
213 ms |
39700 KB |
Output is correct |
103 |
Correct |
299 ms |
39372 KB |
Output is correct |
104 |
Correct |
241 ms |
39484 KB |
Output is correct |
105 |
Correct |
177 ms |
39620 KB |
Output is correct |
106 |
Correct |
207 ms |
39016 KB |
Output is correct |