#include "job.h"
#include <bits/extc++.h>
#include <vector>
#define pb emplace_back
const int maxn = 200010;
using ll = long long;
using namespace std;
int deg[maxn], togo[maxn], tl, n;
ll res;
vector<int> edge[maxn];//, sorted[maxn];
ll p[maxn], u[maxn], d[maxn];
struct cmp {
bool operator()(int a, int b) const {
return d[a] * u[b] > d[b] * u[a];
}
};
__gnu_pbds::priority_queue<int, cmp> sorted[maxn];
void sub_merge(int a, int b) {
res -= (ll)d[b] * u[a];
d[a] += d[b];
u[a] += u[b];
}
void get_ans() {
res = 0;
for (int i = 0;i < n;++i) {
int now = togo[i];
for (int u : edge[now]) {
sorted[now].join(sorted[u]);
// if (sorted[u].size() > sorted[now].size()) swap(sorted[u], sorted[now]);
// int a = sorted[now].size();
// //for (int s : sorted[u]) sorted[now].pb(s);
// sorted[now].insert(sorted[now].end(), sorted[u].begin(), sorted[u].end());
// inplace_merge(sorted[now].begin(), sorted[now].begin()+a, sorted[now].end(), cmp());
}
//sort(sorted[now].begin(), sorted[now].end(), cmp());
while (sorted[now].size() && cmp()(now, sorted[now].top())) {
sub_merge(now, sorted[now].top());
sorted[now].pop();
}
sorted[now].push(now);
// if (now)
// sorted[now].pb(now);
}
auto &v = sorted[0];
//sort(v.begin(), v.end(), cmp());
ll t = 0;
auto dojob = [&](int a) {
//cerr << "dojob " << a << ' ' << "d, u : " << d[a] << ' ' << u[a] << '\n';
t += d[a];
res += t * u[a];
};
//dojob(0);
while (v.size()) {
dojob(v.top());
v.pop();
//v.pop_back();
}
}
//long long bf() {
// static bool did[maxn];
// auto &v = edge[0];
// sort(v.begin(), v.end(), cmp());
// for (int u : v)assert(u);
// ll t = 0;
// ll res = 0;
// auto dojob = [&](int a) {
// t += d[a];
// res += t * u[a];
// };
// dojob(0);
// for (int i = (int)v.size()-1;i >= 0;--i)
// dojob(v[i]);
// return res;
//}
long long scheduling_cost(std::vector<int> p, std::vector<int> u, std::vector<int> d) {
n = p.size();
for (int i = 1;i < n;++i) edge[p[i]].pb(i);
for (int i = 1;i < n;++i) ++deg[ p[i] ];
for (int i = 0;i < n;++i) if (deg[i] == 0)
togo[tl++] = i;
for (int i = 0;i < n;++i) {
int now = togo[i];
if (now == 0) {
assert(n-1 == i);
break;
}
if ( --deg[ p[now] ] == 0) togo[tl++] = p[now];
}
for (int i = 0;i < n;++i)
::p[i] = p[i], ::u[i] = u[i], ::d[i] = d[i];
get_ans();
return res;
//cerr << "my ans is : " << res << '\n';
//res = 0;
//cerr << "brute force : " << bf() << '\n';
//return bf();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
12 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
11 ms |
9856 KB |
Output is correct |
5 |
Correct |
40 ms |
14840 KB |
Output is correct |
6 |
Correct |
84 ms |
19960 KB |
Output is correct |
7 |
Correct |
100 ms |
25080 KB |
Output is correct |
8 |
Correct |
124 ms |
30072 KB |
Output is correct |
9 |
Correct |
124 ms |
30072 KB |
Output is correct |
10 |
Correct |
127 ms |
30048 KB |
Output is correct |
11 |
Correct |
12 ms |
9728 KB |
Output is correct |
12 |
Correct |
140 ms |
30200 KB |
Output is correct |
13 |
Correct |
123 ms |
30200 KB |
Output is correct |
14 |
Correct |
126 ms |
30072 KB |
Output is correct |
15 |
Correct |
122 ms |
30072 KB |
Output is correct |
16 |
Correct |
130 ms |
30328 KB |
Output is correct |
17 |
Correct |
124 ms |
30048 KB |
Output is correct |
18 |
Correct |
123 ms |
30072 KB |
Output is correct |
19 |
Correct |
123 ms |
30072 KB |
Output is correct |
20 |
Correct |
134 ms |
36600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
11 ms |
9728 KB |
Output is correct |
4 |
Correct |
323 ms |
31980 KB |
Output is correct |
5 |
Correct |
261 ms |
31980 KB |
Output is correct |
6 |
Correct |
260 ms |
31852 KB |
Output is correct |
7 |
Correct |
263 ms |
31852 KB |
Output is correct |
8 |
Correct |
251 ms |
31852 KB |
Output is correct |
9 |
Correct |
267 ms |
31980 KB |
Output is correct |
10 |
Correct |
283 ms |
31912 KB |
Output is correct |
11 |
Correct |
302 ms |
31876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
12 ms |
9856 KB |
Output is correct |
5 |
Correct |
18 ms |
10880 KB |
Output is correct |
6 |
Correct |
267 ms |
32368 KB |
Output is correct |
7 |
Correct |
274 ms |
32408 KB |
Output is correct |
8 |
Correct |
322 ms |
32492 KB |
Output is correct |
9 |
Correct |
304 ms |
32492 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
11 ms |
9856 KB |
Output is correct |
12 |
Correct |
20 ms |
10752 KB |
Output is correct |
13 |
Correct |
19 ms |
10880 KB |
Output is correct |
14 |
Correct |
270 ms |
32364 KB |
Output is correct |
15 |
Correct |
281 ms |
32492 KB |
Output is correct |
16 |
Correct |
283 ms |
32620 KB |
Output is correct |
17 |
Correct |
331 ms |
32492 KB |
Output is correct |
18 |
Correct |
285 ms |
32620 KB |
Output is correct |
19 |
Correct |
265 ms |
32364 KB |
Output is correct |
20 |
Correct |
262 ms |
32364 KB |
Output is correct |
21 |
Correct |
303 ms |
32620 KB |
Output is correct |
22 |
Correct |
329 ms |
32364 KB |
Output is correct |
23 |
Correct |
284 ms |
32492 KB |
Output is correct |
24 |
Correct |
338 ms |
32364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
127 ms |
30200 KB |
Output is correct |
3 |
Correct |
122 ms |
30072 KB |
Output is correct |
4 |
Correct |
125 ms |
30072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
11 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
10 ms |
9728 KB |
Output is correct |
7 |
Correct |
11 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9704 KB |
Output is correct |
9 |
Correct |
10 ms |
9728 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
11 ms |
9728 KB |
Output is correct |
13 |
Correct |
11 ms |
9728 KB |
Output is correct |
14 |
Correct |
11 ms |
9728 KB |
Output is correct |
15 |
Correct |
10 ms |
9728 KB |
Output is correct |
16 |
Correct |
11 ms |
9728 KB |
Output is correct |
17 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
12 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
11 ms |
9856 KB |
Output is correct |
5 |
Correct |
40 ms |
14840 KB |
Output is correct |
6 |
Correct |
84 ms |
19960 KB |
Output is correct |
7 |
Correct |
100 ms |
25080 KB |
Output is correct |
8 |
Correct |
124 ms |
30072 KB |
Output is correct |
9 |
Correct |
124 ms |
30072 KB |
Output is correct |
10 |
Correct |
127 ms |
30048 KB |
Output is correct |
11 |
Correct |
12 ms |
9728 KB |
Output is correct |
12 |
Correct |
140 ms |
30200 KB |
Output is correct |
13 |
Correct |
123 ms |
30200 KB |
Output is correct |
14 |
Correct |
126 ms |
30072 KB |
Output is correct |
15 |
Correct |
122 ms |
30072 KB |
Output is correct |
16 |
Correct |
130 ms |
30328 KB |
Output is correct |
17 |
Correct |
124 ms |
30048 KB |
Output is correct |
18 |
Correct |
123 ms |
30072 KB |
Output is correct |
19 |
Correct |
123 ms |
30072 KB |
Output is correct |
20 |
Correct |
134 ms |
36600 KB |
Output is correct |
21 |
Correct |
10 ms |
9728 KB |
Output is correct |
22 |
Correct |
10 ms |
9728 KB |
Output is correct |
23 |
Correct |
11 ms |
9728 KB |
Output is correct |
24 |
Correct |
323 ms |
31980 KB |
Output is correct |
25 |
Correct |
261 ms |
31980 KB |
Output is correct |
26 |
Correct |
260 ms |
31852 KB |
Output is correct |
27 |
Correct |
263 ms |
31852 KB |
Output is correct |
28 |
Correct |
251 ms |
31852 KB |
Output is correct |
29 |
Correct |
267 ms |
31980 KB |
Output is correct |
30 |
Correct |
283 ms |
31912 KB |
Output is correct |
31 |
Correct |
302 ms |
31876 KB |
Output is correct |
32 |
Correct |
10 ms |
9728 KB |
Output is correct |
33 |
Correct |
10 ms |
9728 KB |
Output is correct |
34 |
Correct |
10 ms |
9728 KB |
Output is correct |
35 |
Correct |
12 ms |
9856 KB |
Output is correct |
36 |
Correct |
18 ms |
10880 KB |
Output is correct |
37 |
Correct |
267 ms |
32368 KB |
Output is correct |
38 |
Correct |
274 ms |
32408 KB |
Output is correct |
39 |
Correct |
322 ms |
32492 KB |
Output is correct |
40 |
Correct |
304 ms |
32492 KB |
Output is correct |
41 |
Correct |
10 ms |
9728 KB |
Output is correct |
42 |
Correct |
11 ms |
9856 KB |
Output is correct |
43 |
Correct |
20 ms |
10752 KB |
Output is correct |
44 |
Correct |
19 ms |
10880 KB |
Output is correct |
45 |
Correct |
270 ms |
32364 KB |
Output is correct |
46 |
Correct |
281 ms |
32492 KB |
Output is correct |
47 |
Correct |
283 ms |
32620 KB |
Output is correct |
48 |
Correct |
331 ms |
32492 KB |
Output is correct |
49 |
Correct |
285 ms |
32620 KB |
Output is correct |
50 |
Correct |
265 ms |
32364 KB |
Output is correct |
51 |
Correct |
262 ms |
32364 KB |
Output is correct |
52 |
Correct |
303 ms |
32620 KB |
Output is correct |
53 |
Correct |
329 ms |
32364 KB |
Output is correct |
54 |
Correct |
284 ms |
32492 KB |
Output is correct |
55 |
Correct |
338 ms |
32364 KB |
Output is correct |
56 |
Correct |
11 ms |
9728 KB |
Output is correct |
57 |
Correct |
127 ms |
30200 KB |
Output is correct |
58 |
Correct |
122 ms |
30072 KB |
Output is correct |
59 |
Correct |
125 ms |
30072 KB |
Output is correct |
60 |
Correct |
9 ms |
9728 KB |
Output is correct |
61 |
Correct |
10 ms |
9728 KB |
Output is correct |
62 |
Correct |
11 ms |
9728 KB |
Output is correct |
63 |
Correct |
10 ms |
9728 KB |
Output is correct |
64 |
Correct |
10 ms |
9728 KB |
Output is correct |
65 |
Correct |
10 ms |
9728 KB |
Output is correct |
66 |
Correct |
11 ms |
9728 KB |
Output is correct |
67 |
Correct |
10 ms |
9704 KB |
Output is correct |
68 |
Correct |
10 ms |
9728 KB |
Output is correct |
69 |
Correct |
10 ms |
9728 KB |
Output is correct |
70 |
Correct |
10 ms |
9728 KB |
Output is correct |
71 |
Correct |
11 ms |
9728 KB |
Output is correct |
72 |
Correct |
11 ms |
9728 KB |
Output is correct |
73 |
Correct |
11 ms |
9728 KB |
Output is correct |
74 |
Correct |
10 ms |
9728 KB |
Output is correct |
75 |
Correct |
11 ms |
9728 KB |
Output is correct |
76 |
Correct |
10 ms |
9728 KB |
Output is correct |
77 |
Correct |
11 ms |
9728 KB |
Output is correct |
78 |
Correct |
10 ms |
9728 KB |
Output is correct |
79 |
Correct |
10 ms |
9728 KB |
Output is correct |
80 |
Correct |
10 ms |
9728 KB |
Output is correct |
81 |
Correct |
10 ms |
9728 KB |
Output is correct |
82 |
Correct |
11 ms |
9728 KB |
Output is correct |
83 |
Correct |
11 ms |
9728 KB |
Output is correct |
84 |
Correct |
11 ms |
9728 KB |
Output is correct |
85 |
Correct |
10 ms |
9728 KB |
Output is correct |
86 |
Correct |
12 ms |
9728 KB |
Output is correct |
87 |
Correct |
11 ms |
9728 KB |
Output is correct |
88 |
Correct |
15 ms |
10240 KB |
Output is correct |
89 |
Correct |
17 ms |
10624 KB |
Output is correct |
90 |
Correct |
18 ms |
10880 KB |
Output is correct |
91 |
Correct |
56 ms |
15608 KB |
Output is correct |
92 |
Correct |
112 ms |
21112 KB |
Output is correct |
93 |
Correct |
256 ms |
32568 KB |
Output is correct |
94 |
Correct |
278 ms |
32760 KB |
Output is correct |
95 |
Correct |
315 ms |
32760 KB |
Output is correct |
96 |
Correct |
324 ms |
32632 KB |
Output is correct |
97 |
Correct |
259 ms |
32668 KB |
Output is correct |
98 |
Correct |
225 ms |
33040 KB |
Output is correct |
99 |
Correct |
355 ms |
33456 KB |
Output is correct |
100 |
Correct |
289 ms |
32864 KB |
Output is correct |
101 |
Correct |
261 ms |
32224 KB |
Output is correct |
102 |
Correct |
360 ms |
36472 KB |
Output is correct |
103 |
Correct |
217 ms |
31768 KB |
Output is correct |
104 |
Correct |
197 ms |
31428 KB |
Output is correct |
105 |
Correct |
224 ms |
31484 KB |
Output is correct |
106 |
Correct |
320 ms |
32884 KB |
Output is correct |