5289: [Hnoi2018]排列
分析:
首先将题意转化一下:每个点向a[i]连一条边,构成了一个以0为根节点的树,要求选一个拓扑序,点x是拓扑序中的第i个,那么价值是i*w[x]。让价值最大。
然后贪心:直观的考虑,应该让权值小的尽量靠前,那么依次考虑当前最小的权值,一旦选了它的父节点,那么下一个就会选它。将它和父节点合并,新的权值为平均数,并且记录下siz。推广一下即每次选平均数最小的集合,和父节点所在的集合合并。
证明:如果当前有两个集合x,y,如果x在前面更优,那么$w[x] + w[y] imes siz[x] > w[y] + w[x] imes siz[y]$
$w[x] imes (siz[y] - 1) < w[y] imes (siz[x] - 1 ) $
$frac{w[x]}{siz[x] - 1} < frac{w[y]}{siz[y] - 1}$
所以x在y前面的条件是平均数小,那么就可以用堆来维护了。注意一下如果平均数比较的话,要开long double,或者直接按照上面的第二个式子来比较,不存在精度问题。
代码:
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #include<cctype> #include<set> #include<queue> #include<vector> #include<map> using namespace std; typedef long long LL; inline int read() { int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1; for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f; } #define pa pair<long double,int> const int N = 500005; int fa[N], a[N], siz[N]; LL w[N]; struct Heap{ priority_queue< pa, vector< pa >, greater< pa > > a, b; void Insert(pa x) { a.push(x); } void Delete(pa x) { b.push(x); } pa Top() { while (!b.empty() && a.top() == b.top()) a.pop(), b.pop(); return a.top(); } }q; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } int main() { int n = read(); for (int i = 0; i <= n; ++i) fa[i] = i; for (int i = 1; i <= n; ++i) { a[i] = read(); int u = find(a[i]), v = find(i); if (u != v) fa[u] = v; else { puts("-1"); return 0; } } LL ans = 0; for (int i = 0; i <= n; ++i) fa[i] = i; for (int i = 1; i <= n; ++i) { w[i] = read(); siz[i] = 1; ans += w[i]; q.Insert(pa(w[i], i)); } for (int k = 1; k <= n; ++k) { pa now = q.Top(); q.Delete(now); int x = now.second, y = find(a[x]); if (y) q.Delete(pa((long double)w[y] / siz[y], y)); ans += 1ll * siz[y] * w[x]; w[y] += w[x]; siz[y] += siz[x]; fa[x] = y; if (y) q.Insert(pa((long double)w[y] / siz[y], y)); } cout << ans; return 0; }