#include "job.h"
#include <bits/stdc++.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];
}
};
void sub_merge(int a, int b) {
res -= (ll)d[b] * u[a];
d[a] += d[b];
u[a] += u[b];
}
//__gnu_pbds::priority_queue<int, cmp> pq[maxn];
void get_ans() {
for (int i = 0;i < n;++i) {
int now = togo[i];
//cerr << "now " << now << '\n';
assert(now || i == n-1);
for (int u : edge[now]) {
if (sorted[u].size() > sorted[now].size()) swap(sorted[u], sorted[now]);
int a = sorted[now].size();
sorted[now].insert(sorted[now].begin(), sorted[u].begin(), sorted[u].end());
inplace_merge(sorted[now].begin(), sorted[now].begin()+sorted[u].size(), sorted[now].end(), cmp());
}
while (sorted[now].size() && cmp()(now, sorted[now].back())) {
sub_merge(now, sorted[now].back());
sorted[now].pop_back();
}
sorted[now].pb(now);
}
auto &v = sorted[0];
ll t = 0;
auto dojob = [&](int a) {
//cerr << "dojob " << a << ' ' << "d, u : " << d[a] << ' ' << u[a] << '\n';
t += d[a];
res += t * u[a];
};
while (v.size()) {
dojob(v.back());
v.pop_back();
}
}
//
//void dfs(int now) {
// for (int u : edge[i]) dfs(u);
// sort(edge[now].begin(), edge[now].end(), cmp());
// vector<int> down;
// while (edge[now].size() && cmp(now, edge[now].back())) {
// for (int u : merge(now, edge[now].back()))
// down.pb(u);
// edge[now].pop_back();
// }
// for (int u : down) edge[now].pb(u);
//}
//
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) 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;
}
Compilation message
job.cpp: In function 'void get_ans()':
job.cpp:30:8: warning: unused variable 'a' [-Wunused-variable]
int a = sorted[now].size();
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9856 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 KB |
Output is correct |
5 |
Correct |
37 ms |
14072 KB |
Output is correct |
6 |
Correct |
67 ms |
18296 KB |
Output is correct |
7 |
Correct |
87 ms |
22648 KB |
Output is correct |
8 |
Correct |
118 ms |
27016 KB |
Output is correct |
9 |
Correct |
117 ms |
26872 KB |
Output is correct |
10 |
Correct |
116 ms |
27000 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
117 ms |
27000 KB |
Output is correct |
13 |
Correct |
112 ms |
27000 KB |
Output is correct |
14 |
Correct |
120 ms |
27000 KB |
Output is correct |
15 |
Correct |
114 ms |
27000 KB |
Output is correct |
16 |
Correct |
120 ms |
27000 KB |
Output is correct |
17 |
Correct |
123 ms |
27088 KB |
Output is correct |
18 |
Correct |
121 ms |
27036 KB |
Output is correct |
19 |
Correct |
111 ms |
26872 KB |
Output is correct |
20 |
Correct |
129 ms |
28144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9984 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Execution timed out |
3081 ms |
27784 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
11 ms |
9856 KB |
Output is correct |
5 |
Correct |
72 ms |
10744 KB |
Output is correct |
6 |
Execution timed out |
3081 ms |
27588 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
119 ms |
27004 KB |
Output is correct |
3 |
Correct |
117 ms |
26952 KB |
Output is correct |
4 |
Correct |
126 ms |
26976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
12 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 |
12 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 |
11 ms |
9728 KB |
Output is correct |
9 |
Correct |
10 ms |
9748 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
10 ms |
9728 KB |
Output is correct |
13 |
Correct |
11 ms |
9704 KB |
Output is correct |
14 |
Correct |
11 ms |
9728 KB |
Output is correct |
15 |
Correct |
11 ms |
9728 KB |
Output is correct |
16 |
Correct |
10 ms |
9728 KB |
Output is correct |
17 |
Correct |
11 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9856 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 KB |
Output is correct |
5 |
Correct |
37 ms |
14072 KB |
Output is correct |
6 |
Correct |
67 ms |
18296 KB |
Output is correct |
7 |
Correct |
87 ms |
22648 KB |
Output is correct |
8 |
Correct |
118 ms |
27016 KB |
Output is correct |
9 |
Correct |
117 ms |
26872 KB |
Output is correct |
10 |
Correct |
116 ms |
27000 KB |
Output is correct |
11 |
Correct |
10 ms |
9728 KB |
Output is correct |
12 |
Correct |
117 ms |
27000 KB |
Output is correct |
13 |
Correct |
112 ms |
27000 KB |
Output is correct |
14 |
Correct |
120 ms |
27000 KB |
Output is correct |
15 |
Correct |
114 ms |
27000 KB |
Output is correct |
16 |
Correct |
120 ms |
27000 KB |
Output is correct |
17 |
Correct |
123 ms |
27088 KB |
Output is correct |
18 |
Correct |
121 ms |
27036 KB |
Output is correct |
19 |
Correct |
111 ms |
26872 KB |
Output is correct |
20 |
Correct |
129 ms |
28144 KB |
Output is correct |
21 |
Correct |
11 ms |
9984 KB |
Output is correct |
22 |
Correct |
11 ms |
9728 KB |
Output is correct |
23 |
Correct |
10 ms |
9728 KB |
Output is correct |
24 |
Execution timed out |
3081 ms |
27784 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |