UVa 11300 Spreading the Wealth
来源:互联网 发布:人工智能就业前景 编辑:程序博客网 时间:2024/05/17 03:28
本题是数学题
1.数学题关键在于建立数学模型。
2.把握中位数。
考虑第i位和第i+1位,有可能是最终i给i+1也可能是i+1给i,假设i给i+1个人则用正数表示,i+1个人给第i个人则用负数表示。则可以建立起数学模型。
设起始每一个人有P个金币,给出去X个金币。设最终每个人用于M个金币。
则有
P1-X1+Xn = M
P2-X2+X1 = M
....
Pn-Xn+X1 = M;
可得
X2 = X1+P2 - M =X1 - C1;
X3 = X2+P2 - M =X1 - C2;
...
Xn = X2+Pn - M = X1-Cn-1;
则最终总移动的次数为X1+|X1-C1| +...|X1-Cn-1|
这里用到中位数,C1到Cn-1相当于数轴上的N个点,求X1到N个点的距离最小,则X必在中位数位置上!
#include <cstdlib>#include <iostream>#include <algorithm>#include <stdio.h>using namespace std;const int MAX = 1000000+5;long long p[MAX],c[MAX],sum;int main(int argc, char *argv[]){ int n; while(scanf("%d",&n)!=-1) { sum = 0; for(int i = 0; i < n; i++) { scanf("%lld",p+i); sum += p[i]; } long long avg = sum/n; c[0] = 0; for(int i = 1; i < n; i++) { c[i] = c[i-1] + p[i] - avg; } sort(c+1,c+n); long long mid_num = c[n/2]; long long result = mid_num; for(int i = 1; i < n; i++)result += abs(mid_num - c[i]); printf("%lld\n",result); } //system("PAUSE"); return EXIT_SUCCESS;}
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
- 在linux下使用c语言操作临时文件
- 一个登录的小异常 哈哈哈
- 2014蓝桥杯本科B组C/C++第一题【啤酒和饮料】
- Android复习之Menu
- HDU 2457 DNA repair 不含模式串的最少修改次数
- UVa 11300 Spreading the Wealth
- hdu 1269
- ora.orcl.db ora....se.type OFFLINE OFFLINE
- Android EditText自定义按键盘
- 纯JS写的2048游戏,分享之
- PAT - 1006. 换个格式输出整数 (15)
- WCHAR和CHAR的常用处理函数
- poj 1742 Coins
- 你如何评价Android系统?优缺点