#include<bits/stdc++.h>
using namespace std;
long long D[200001],U[200001];
vector<int> adj[200001];
bool comp(int a,int b){
return (D[a]*U[b])<(D[b]*U[a]);
}
long long scheduling_cost(vector<int> p,vector<int> u,vector<int> d){
int n = p.size();
int real[n];
long long nxt[n];
U[0] = u[0] , D[0] = d[0];
real[0] = 1;nxt[0] = -1;
for(int i = 1;i<n;i++){
real[i] = 1;
nxt[i] = -1;
U[i] = u[i];
D[i] = d[i];
adj[p[i]].push_back(i);
}
for(int i = n-1;i>=0;i--){
while(adj[i].size()){
int x = -1;
if(comp(adj[i].back(),i)){
x = adj[i].back();
}
adj[i].pop_back();
if(x==-1)continue;
real[x] = 0;
nxt[i] = x;
D[i]+=D[x];
U[i]+=U[x];
if(adj[i].size()<adj[x].size())swap(adj[i],adj[x]);
while(adj[x].size()){
adj[i].push_back(adj[x].back());adj[x].pop_back();
}
}
}
vector<int> ans;
for(int i = 0;i<n;i++){
if(real[i]){
ans.push_back(i);
}
}
sort(ans.begin(),ans.end());
long long all = 0 , cur = 0;
for(int i = 0;i<ans.size();i++){
int lol = ans[i];
while(lol!=-1){
cur+=d[lol];
all+=cur*u[lol];
lol = nxt[lol];
}
}
return all;
}
/*
int main(){
cout<<scheduling_cost({-1,0,0},{5,2,5},{3,4,1});
}*/
Compilation message
job.cpp: In function 'long long int scheduling_cost(std::vector<int>, std::vector<int>, std::vector<int>)':
job.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int i = 0;i<ans.size();i++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5008 KB |
Output is correct |
3 |
Correct |
3 ms |
5004 KB |
Output is correct |
4 |
Correct |
2 ms |
5024 KB |
Output is correct |
5 |
Correct |
20 ms |
10048 KB |
Output is correct |
6 |
Correct |
33 ms |
15116 KB |
Output is correct |
7 |
Correct |
50 ms |
20036 KB |
Output is correct |
8 |
Correct |
80 ms |
25252 KB |
Output is correct |
9 |
Correct |
63 ms |
25244 KB |
Output is correct |
10 |
Correct |
75 ms |
25240 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
65 ms |
25260 KB |
Output is correct |
13 |
Correct |
64 ms |
25252 KB |
Output is correct |
14 |
Correct |
67 ms |
25132 KB |
Output is correct |
15 |
Correct |
62 ms |
24524 KB |
Output is correct |
16 |
Correct |
64 ms |
25128 KB |
Output is correct |
17 |
Correct |
63 ms |
25244 KB |
Output is correct |
18 |
Correct |
65 ms |
25248 KB |
Output is correct |
19 |
Correct |
73 ms |
24492 KB |
Output is correct |
20 |
Correct |
64 ms |
25760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
5012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
5008 KB |
Output is correct |
3 |
Correct |
3 ms |
5004 KB |
Output is correct |
4 |
Correct |
2 ms |
5024 KB |
Output is correct |
5 |
Correct |
20 ms |
10048 KB |
Output is correct |
6 |
Correct |
33 ms |
15116 KB |
Output is correct |
7 |
Correct |
50 ms |
20036 KB |
Output is correct |
8 |
Correct |
80 ms |
25252 KB |
Output is correct |
9 |
Correct |
63 ms |
25244 KB |
Output is correct |
10 |
Correct |
75 ms |
25240 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
65 ms |
25260 KB |
Output is correct |
13 |
Correct |
64 ms |
25252 KB |
Output is correct |
14 |
Correct |
67 ms |
25132 KB |
Output is correct |
15 |
Correct |
62 ms |
24524 KB |
Output is correct |
16 |
Correct |
64 ms |
25128 KB |
Output is correct |
17 |
Correct |
63 ms |
25244 KB |
Output is correct |
18 |
Correct |
65 ms |
25248 KB |
Output is correct |
19 |
Correct |
73 ms |
24492 KB |
Output is correct |
20 |
Correct |
64 ms |
25760 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |