#include<bits/stdc++.h>
using namespace std;
long long D[200001],U[200001];
bool comp(int a,int b){
return (D[a]*U[b])<(D[b]*U[a]);
}
struct cmp {
bool operator()(const int &a, const int &b) {
return (D[a]*U[b])>(D[b]*U[a]);
}
};
priority_queue<int,vector<int>,cmp> adj[200001];
long long scheduling_cost(vector<int> p,vector<int> u,vector<int> d){
int n = p.size();
int real[n];
long long nxt[n],nick_name[n];
U[0] = u[0] , D[0] = d[0];
real[0] = 1;nxt[0] = -1;
nick_name[0] = 0;
for(int i = 1;i<n;i++){
nick_name[i] = i;
real[i] = 1;
nxt[i] = -1;
U[i] = u[i];
D[i] = d[i];
adj[p[i]].push(i);
}
for(int i = n-1;i>=0;i--){
while(adj[i].size()){
int x = -1;
if(comp(adj[i].top(),i)){
x = adj[i].top();
}
adj[i].pop();
if(x==-1)continue;
real[x] = 0;
nxt[nick_name[i]] = x;
nick_name[i] = nick_name[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(adj[x].top());adj[x].pop();
}
}
}
vector<int> ans;
for(int i = 0;i<n;i++){
if(real[i]){
ans.push_back(i);
}
}
sort(ans.begin(),ans.end(),comp);
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:56:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i = 0;i<ans.size();i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
4 ms |
6568 KB |
Output is correct |
4 |
Correct |
148 ms |
21540 KB |
Output is correct |
5 |
Correct |
130 ms |
21536 KB |
Output is correct |
6 |
Correct |
154 ms |
21524 KB |
Output is correct |
7 |
Correct |
150 ms |
21476 KB |
Output is correct |
8 |
Correct |
144 ms |
21640 KB |
Output is correct |
9 |
Correct |
131 ms |
21440 KB |
Output is correct |
10 |
Correct |
132 ms |
21524 KB |
Output is correct |
11 |
Correct |
132 ms |
21528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
4 ms |
6564 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
8 ms |
7380 KB |
Output is correct |
6 |
Correct |
137 ms |
21532 KB |
Output is correct |
7 |
Correct |
137 ms |
21536 KB |
Output is correct |
8 |
Correct |
138 ms |
21524 KB |
Output is correct |
9 |
Correct |
142 ms |
21532 KB |
Output is correct |
10 |
Correct |
3 ms |
6568 KB |
Output is correct |
11 |
Correct |
4 ms |
6612 KB |
Output is correct |
12 |
Correct |
7 ms |
7216 KB |
Output is correct |
13 |
Correct |
7 ms |
7380 KB |
Output is correct |
14 |
Correct |
137 ms |
21524 KB |
Output is correct |
15 |
Correct |
141 ms |
21528 KB |
Output is correct |
16 |
Correct |
157 ms |
21460 KB |
Output is correct |
17 |
Correct |
139 ms |
21484 KB |
Output is correct |
18 |
Correct |
138 ms |
21524 KB |
Output is correct |
19 |
Correct |
154 ms |
21532 KB |
Output is correct |
20 |
Correct |
139 ms |
21408 KB |
Output is correct |
21 |
Correct |
140 ms |
21524 KB |
Output is correct |
22 |
Correct |
137 ms |
21528 KB |
Output is correct |
23 |
Correct |
171 ms |
21532 KB |
Output is correct |
24 |
Correct |
137 ms |
21464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Incorrect |
67 ms |
25352 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Incorrect |
3 ms |
6564 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |