# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
144780 | gmfabat | Combo (IOI18_combo) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "job.h"
#define ll long long
using namespace std;
const int N = 200005;
int a[N], fa[N], dd[N],sz[N]; ll w[N];
struct node {
int id; ll son, mom;
node (int _id = 0, ll _son = 0, ll _mom = 0) {id = _id, son = _son; mom = _mom; }
bool operator < (const node &b) const {return son*b.mom < b.son*mom; }
};
priority_queue<node>Q;
int find(int o) {return ~fa[o] ? fa[o] = find(fa[o]) : o;}
ll scheduling_cost(std::vector<int> p, std::vector<int> u, std::vector<int> d)
{
int n=p.size();
memset(fa, -1, sizeof(fa));
for (int i = 1; i <= n; i++) { //初始化index , 由1-N 而不是0- N-1, 其實也可不做 , 但下面要小改, 注意index
a[i]=p[i-1]+1;
w[i]=u[i-1];
dd[i]=d[i-1];
}
for (int i = 1; i <= n; i++)
if (find(a[i])^find(i)) fa[find(a[i])] = find(i);
long long ans = 0; int loc = 0;
for (int i = 1; i <= n; i++) scanf("%lld", &w[i]);
for (int i = 1; i <= n; i++) {
scanf("%d", &dd[i]);
Q.push(node(i, w[i], dd[i])); //建堆
sz[i] = dd[i];
ans += w[i]*dd[i];
}
memset(fa, -1, sizeof(fa));
while (!Q.empty()) { // 每次合二個點合二為一, 消一個點和對應邊 , 消到堆為空為止
node t = Q.top(); Q.pop();
if (sz[t.id] != t.mom) continue;
if (find(a[t.id]) == 0) {
ans += w[t.id]*loc; fa[t.id] = 0; loc += sz[t.id];
}else {
int tmp = find(a[t.id]);
ans += w[t.id]*sz[tmp], fa[t.id] = tmp;
w[tmp] += w[t.id], sz[tmp] += sz[t.id];
Q.push(node(tmp, w[tmp], sz[tmp]));
}
}
return ans;
}