#include "job.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int mxN=2e5;
int n;
vector<int> adj[mxN], p, u, d;
bool was[mxN];
ll ans=0;
struct cmp{
bool operator()(int x, int y) {
return (ll)d[x]*(ll)u[y]>(ll)d[y]*(ll)u[x];
}
};
priority_queue<int, vector<int>, cmp> q[mxN];
void merge(int x, int y) {
d[x]+=d[y];
u[x]+=u[y];
}
void dfs(int x) {
assert(x>=0&&x<n);
for(int v:adj[x])
dfs(v), q[x].push(v);
while(!q[x].empty()) {
int y=q[x].top();
if((ll)d[y]*(ll)u[x]>(ll)d[x]*(ll)u[y])
break;
was[y]=true;
q[x].pop();
ans-=d[y]*u[x];
merge(x, y);
if((int)q[x].size()<(int)q[y].size())
swap(q[x], q[y]);
while(!q[y].empty())
q[x].push(q[y].top()), q[y].pop();
}
}
ll scheduling_cost(vector<int> p1, vector<int> u1, vector<int> d1) {
p=p1, u=u1, d=d1;
n = (int) p.size();
for(int i=1; i<n; ++i)
adj[p1[i]].pb(i);
dfs(0);
vector<int> j;
for(int i=0; i<n; ++i)
if(!was[i])
j.pb(i);
sort(j.rbegin(), j.rend(), [&](int x, int y) {
return (ll)d[x]*u[y]>(ll)d[y]*u[x];
});
ll t=0LL;
for(int i:j)
t+=d[i], ans+=(ll)u[i]*t;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Incorrect |
8 ms |
11244 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Correct |
8 ms |
11244 KB |
Output is correct |
4 |
Correct |
108 ms |
21600 KB |
Output is correct |
5 |
Correct |
108 ms |
21600 KB |
Output is correct |
6 |
Correct |
113 ms |
21728 KB |
Output is correct |
7 |
Correct |
110 ms |
21728 KB |
Output is correct |
8 |
Correct |
107 ms |
21472 KB |
Output is correct |
9 |
Correct |
109 ms |
21600 KB |
Output is correct |
10 |
Correct |
108 ms |
21600 KB |
Output is correct |
11 |
Correct |
109 ms |
21600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Correct |
8 ms |
11244 KB |
Output is correct |
4 |
Correct |
8 ms |
11372 KB |
Output is correct |
5 |
Correct |
13 ms |
11884 KB |
Output is correct |
6 |
Correct |
123 ms |
23904 KB |
Output is correct |
7 |
Correct |
118 ms |
23904 KB |
Output is correct |
8 |
Correct |
118 ms |
23904 KB |
Output is correct |
9 |
Correct |
120 ms |
23904 KB |
Output is correct |
10 |
Correct |
8 ms |
11244 KB |
Output is correct |
11 |
Correct |
9 ms |
11372 KB |
Output is correct |
12 |
Correct |
12 ms |
11884 KB |
Output is correct |
13 |
Correct |
14 ms |
11884 KB |
Output is correct |
14 |
Correct |
116 ms |
23904 KB |
Output is correct |
15 |
Correct |
118 ms |
23904 KB |
Output is correct |
16 |
Correct |
117 ms |
23904 KB |
Output is correct |
17 |
Correct |
118 ms |
24028 KB |
Output is correct |
18 |
Correct |
116 ms |
23776 KB |
Output is correct |
19 |
Correct |
117 ms |
23904 KB |
Output is correct |
20 |
Correct |
122 ms |
24032 KB |
Output is correct |
21 |
Correct |
117 ms |
23904 KB |
Output is correct |
22 |
Correct |
118 ms |
23904 KB |
Output is correct |
23 |
Correct |
119 ms |
24032 KB |
Output is correct |
24 |
Correct |
121 ms |
23776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Incorrect |
123 ms |
42220 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11276 KB |
Output is correct |
2 |
Correct |
7 ms |
11244 KB |
Output is correct |
3 |
Incorrect |
8 ms |
11244 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Incorrect |
8 ms |
11244 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |