[BZOJ] 1045: [HAOI2008] 糖果传递
贪心,中位数
#include<iostream> #include<cstdio> using namespace std; long long a[1000005],s[1000005]; int n; intmain(){ long long ave=0,ans=0; scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%lld",a+i); for(inti=1;i<=n;++i) ave+=a[i]; ave/=n; for(int i=1;i<=n;++i) s[i]=s[i-1]+a[i]-ave; sort(s+1,s+n+1); for(int i=1;i<n/2+1;++i) ans+=s[n/2+1]-s[i]; for(int i=n/2+1;i<=n;++i) ans+=s[i]-s[n/2+1]; printf("%lld\n",ans); return0; }
本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9392754.html