//#include "job.h"
#include <bits/stdc++.h>
using namespace std;
long long scheduling_cost(std::vector<int> p, std::vector<int> u, std::vector<int> d) {
int n = p.size();
struct node {
long long u, d;
int id;
bool operator<(node p)const{
return u*p.d < d*p.u;
}
};
long long ans = 0;
vector<int> pa(n);
iota(pa.begin(), pa.end(), 0);
function<int(int)> anc = [&](int x){
return x==pa[x] ? x : pa[x]=anc(pa[x]);
};
auto add = [&](int a, int i) {
//cerr << a << ' ' << i << '\n';
ans -= d[i] * u[a];
//cerr << "ans="<<ans << '\n';
d[a] += d[i];
u[a] += u[i];
};
priority_queue<node> pq;
for(int i = 0; i < n; i++)
pq.push({u[i],d[i],i});
while(pq.size()) {
node x;
do x=pq.top(), pq.pop(); while(pq.size() && (x.u!=u[x.id] || x.d!=d[x.id]));
int i = x.id;
if(p[i] == -1 || pa[i] != i) continue;
//cerr << "u,d = " << x.u << ' ' << x.d << '\n';
//cerr << "i = " << i << '\n';
int a = anc(p[i]);
pa[i] = a;
add(a, i);
//cout << "a: " << a << ' ' << u[a] << ' ' << d[a] << '\n';
pq.push({u[a],d[a],a});
}
//cerr << "ans = " << ans << '\n';
//cerr << u[0] << ' ' << d[0] << '\n';
ans += 1LL * u[0] * d[0];
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
196 ms |
12136 KB |
Output is correct |
5 |
Correct |
181 ms |
12136 KB |
Output is correct |
6 |
Correct |
183 ms |
12136 KB |
Output is correct |
7 |
Correct |
195 ms |
12136 KB |
Output is correct |
8 |
Correct |
184 ms |
12136 KB |
Output is correct |
9 |
Correct |
184 ms |
12136 KB |
Output is correct |
10 |
Correct |
184 ms |
12136 KB |
Output is correct |
11 |
Correct |
184 ms |
12136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Incorrect |
4 ms |
256 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Incorrect |
251 ms |
12136 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Incorrect |
4 ms |
256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |