bzoj3293/1045 [Cqoi2011]分金币/[HAOI2008] 糖果传递 贪心

来源:互联网 发布:战斗妖精雪风小说淘宝 编辑:程序博客网 时间:2024/05/17 23:57

圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除。每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等。你的任务是求出被转手的金币数量的最小值。n<=1e6.

这个是刘汝佳蓝书P4的原题,还蛮巧妙的。

证明这里也有

http://blog.csdn.net/ycdfhhc/article/details/45437677

#include<bits/stdc++.h>#define LL long long#define clr(x,i) memset(x,i,sizeof(x))using namespace std;const int N=1000005;LL n,a[N],c[N],tot;int main(){scanf("%lld",&n);for(int i=1;i<=n;i++){scanf("%lld",&a[i]);tot+=a[i];}tot/=n;for(int i=2;i<=n;i++)  c[i]=c[i-1]+a[i-1]-tot;sort(c+1,c+n+1);LL m=c[(n+1)/2],ans=0;for(int i=1;i<=n;i++)  ans+=abs(c[i]-m);printf("%lld",ans);return 0;}


阅读全文
0 0
原创粉丝点击