UVA 11300 Spreading the Wealth(数学)
来源:互联网 发布:弘讯科技人工智能 编辑:程序博客网 时间:2024/05/16 01:04
题目大意:
圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除。每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等。你的任务是求出被转手的金币数量的最小值。比如,n=4,且4个人的金币数量分别为1,2,5,4时,只需转移4枚金币(第3个人给第2个人两枚金币,第2个人和第4个人分别给第1个人1枚金币)即可实现每人手中的金币数目相等。
解析:
原来金币数为a1,a2,...an.
最终的金币为这些数的平均值,设为A
xi表示i给i+1的金币个数
a1+xn-x1=A
a2+x1-x2=A
a3+x2-x3=A
a4+x3-x4=A
......
an+x(n-1)-xn=A
利用后面n-1个等式,用x1表示 x2,x3,...xn
x2=x1-(A-a2)
x3=x1-(2*A-a2-a3);
...
结果就是求|x1|+|x1-b1|+|x1-b2|+...+|x1-b[n-1]|的最小值,取中位数
圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除。每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等。你的任务是求出被转手的金币数量的最小值。比如,n=4,且4个人的金币数量分别为1,2,5,4时,只需转移4枚金币(第3个人给第2个人两枚金币,第2个人和第4个人分别给第1个人1枚金币)即可实现每人手中的金币数目相等。
解析:
原来金币数为a1,a2,...an.
最终的金币为这些数的平均值,设为A
xi表示i给i+1的金币个数
a1+xn-x1=A
a2+x1-x2=A
a3+x2-x3=A
a4+x3-x4=A
......
an+x(n-1)-xn=A
利用后面n-1个等式,用x1表示 x2,x3,...xn
x2=x1-(A-a2)
x3=x1-(2*A-a2-a3);
...
结果就是求|x1|+|x1-b1|+|x1-b2|+...+|x1-b[n-1]|的最小值,取中位数
#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;typedef long long ll;const int N = 1000005;int n;ll a[N],c[N];int main() {while(scanf("%d",&n) != EOF) {ll sum = 0 ,aver;for(int i = 0; i < n; i++) {scanf("%lld",&a[i]);sum += a[i];}aver = sum / n;c[0] = 0;for(int i = 1; i < n; i++) {c[i] = c[i-1] + (aver - a[i]);}sort(c,c+n);ll mid = c[n/2];sum = 0;for(int i = 0; i < n; i++) {sum += abs(mid - c[i]);}printf("%lld\n",sum);}return 0;}
0 0
- UVA 11300 Spreading the Wealth(数学)
- uva 11300Spreading 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
- Android 初步 集成接口的多按键监听单按键监听、布局按键监听 找茬游戏
- 日志2015.2.4
- KMP算法
- iOS开发隐藏键盘方法总结
- oschina安卓源代码解析(六)主页解析 1、登陆
- UVA 11300 Spreading the Wealth(数学)
- IOS操作SQLite
- 【操作系统】第一遍宏观总结
- 2015.02.04——张佳莉
- HTML5/jQuery界面控件:Ignite UI
- 【BZOJ】【P3786】【星系探索】【题解】【(伪)Top tree】
- No resource found that matches the given name 'android:Widget.Material.A
- scanf()函数的用法
- Android 中的Handler