UVa 11300 Spreading the wealth
来源:互联网 发布:材料统计软件 编辑:程序博客网 时间:2024/06/05 03:40
题解:
显然我们这里可以设一些方程,因为我们最后要让每一个人的金币数相等,那么我们现在可以假设每个人向左边的人给了多少金币,比如我们令
那么我们显然可以得到如下方程
令
显然我们可以通过这些方程来进行求解,具体的方式是,我们把所有的
这里我们让
显然
所以把所有的
显然最后变成了
代码:
#include<cstdio>#include<cmath>#include<algorithm>using namespace std;long long a[1000000+10],sum,M,c[1000000+10];int n;int main(){ while(scanf("%d",&n)!=EOF&&n){sum=0; for(register int i=1;i<=n;i++)scanf("%lld",&a[i]),sum+=a[i]; M=sum/n;c[0]=0; for(register int i=1;i<=n-1;i++)c[i]=c[i-1]+a[i]-M; sort(c+0,c+n);long long num=c[n/2]; long long ans=0;for(register int i=0;i<=n-1;i++)ans+=abs(num-c[i]); printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- Uva - 11300 - Spreading the Wealth
- uva 11300 - Spreading the Wealth
- Uva-11300-Spreading the Wealth
- UVA 11300 Spreading the Wealth
- UVa 11300 - Spreading the Wealth
- uva 11300 Spreading the Wealth
- UVA 11300 Spreading the Wealth
- uva 11300 Spreading the wealth
- UVA - 11300 Spreading the Wealth
- UVa 11300 Spreading the Wealth
- UVA 11300 Spreading the Wealth
- UVa 11300 Spreading the Wealth
- UVA 11300 Spreading the Wealth
- UVA 11300 - Spreading the Wealth
- UVA - 11300 Spreading the Wealth
- UVA 11300-Spreading the Wealth
- UVA 11300 Spreading the Wealth
- UVA 11300 Spreading the Wealth
- 【如果你只是想找个炮友,请把这三…
- 【朋友难过时,别安慰!安慰,其实…
- JAVA设计模式(十三)----责任链模式
- SIP协议中Tag和Branch的概念
- 【你所见的只是冰山一角】根据女童…
- UVa 11300 Spreading the wealth
- bash函数
- 【爱你的人自然会陪著你,不爱的走…
- 【别因为爱太久,就变了。幸福是珍…
- 【单身又如何?至少我过得很好!】…
- 【生活好累!教育真的对我有帮助吗…
- GCD---关于iOS多线程
- 【地震发生时在家最常犯的7个错误&…
- 字符串进制之间的转化