UVA 11300 Spreading the Wealth
来源:互联网 发布:中国特色社会主义 知乎 编辑:程序博客网 时间:2024/06/05 18:44
首先对该题进行数学建模,
令x2为2号给了一号多少金币
2号最终剩余M个金币,M是最后每个人应该剩下的金币
M=a[2]-x2+x3;
同理M=a[3]-x3+x4;
M=a[1]+x2-x1;
x2=M-a[1]+x1;
x3也可以用x1表示,这就是数学建模,建模完成就开始递推就行了。
还有一个技巧,一堆数都减一个数,这个数是他们的中位数,和最小。
#include<stdio.h>#include<cstring>#include<vector>#include<algorithm>using namespace std;//ios::sync_with_stdio(false);const int maxn=1000009;long long a[maxn],c[maxn];int main(){ int n; while(~scanf("%d",&n)) { long long tot=0; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); tot+=a[i]; } long long M=tot/n; c[0]=0; for(int i=1;i<n;i++)c[i]=c[i-1]+a[i]-M; sort(c,c+n); long long x1=c[n/2]; long long ans=0; for(int i=0;i<n;i++)ans+=abs(x1-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
- Hadoop环境搭建
- Brackets 常用插件, 汉化方法及其配置
- “The left-hand side of an assignment must be a variable” problem with charAt
- jQuery datatables插件
- android O正式包即将发布,你准备好了吗
- UVA 11300 Spreading the Wealth
- 求大佬解释下ptrace的pt_regs的定义,而且分为i386 arm x86和aarch64的不同分支
- HttpClient的GET和POST解析
- mysql导出所有用户权限脚本
- 《数据库SQL实战》从titles表获取按照title进行分组
- 数据库系统原理
- HDU 3974 Assign the task——线段树
- 谷歌三大论文之一-Google File System
- HDU2859 Phalanx dp