Uva 11300 Spreading the Wealth(递推,中位数)

简介: Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table.

Spreading the Wealth

Problem

A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village. Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, such that in the end, everyone has the same number of coins. Given the number of coins of each person, compute the minimum number of coins that must be transferred using this method so that everyone has the same number of coins.

The Input

There is a number of inputs. Each input begins with n(n<1000001), the number of people in the village. n lines follow, giving the number of coins of each person in the village, in counterclockwise order around the table. The total number of coins will fit inside an unsigned 64 bit integer.

The Output

For each input, output the minimum number of coins that must be transferred on a single line.

Sample Input

3
100
100
100
4
1
2
5
4

Sample Output

0
4

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2275

题意:圆桌坐着N个人,每个人有一定的金币,金币总数能被N整除。每个人能给左右相邻的人一些金币,最终使得每个人的金币数目相等,求被转手金币数量的最小值。

设 xi表示i号给i-1号xi金币,若xi为负,这表示i-1号给i号(-xi)个金币

Ai表示i号一开始持有的金币

则:对与第1个人:A1-X1+X2=M  ===>X2=X1-(A1-M);令C1=A1-M

对于第2个人:A2-X2+X3=M ====>x3=x2-(A2-M) ====>x3=x1-(A1+A2-2M);===>x3=x1-C2;

……

对于第n个人:An-Xn+x1=M 这个是个恒等式,无用;

所以我们的答案应该是 |X1|+|X2|+|X3|+……+|Xn|的最小值;

====>|X1|+|X1-C1|+|X1-C2|+……+|Xn-1-Cn|的最小值

故当X1取C数组的中间值时,结果最小。。。

注意:|X1 – Ci|在数轴上就是x1到Ci的距离,所以问题变成了:给定数轴上的n个点,找出一个到它们的距离之和尽量小的点。

这个最优的X1就是这些数的“中位数”。即排序以后位于中间的数。至于证明大家自己搜索吧~

下面给出AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1000005;
 4 long long A[maxn],C[maxn],tot,M;
 5 int main()
 6 {
 7     int n;
 8     while(scanf("%d",&n)==1)//这里写EOF会超时,我也不知道咋回事
 9     {
10         tot=0;
11         for(int i=1;i<=n;i++)
12         {
13             scanf("%lld",&A[i]);
14             tot+=A[i];
15         }
16         M=tot/n;//求平均值
17         C[0]=0;
18         for(int i=1;i<n;i++)
19             C[i]=C[i-1]+A[i]-M;//有个递推公式,数学的博大精深啊!
20         sort(C,C+n);
21         long long x1=C[n/2];
22         long long ans=0;
23         for(int i=0;i<n;i++)
24             ans+=abs(x1-C[i]);
25         printf("%lld\n",ans);
26     }
27     return 0;
28 }

 

目录
相关文章
【AcWing】曼哈顿距离
【AcWing】曼哈顿距离
65 0
AcWing 658. 一元二次方程公式
AcWing 658. 一元二次方程公式
88 0
AcWing 658. 一元二次方程公式
|
Java 机器学习/深度学习
UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 65817    Accepted Submission(s): 28794 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。
1148 0
|
算法 JavaScript C语言
洛谷 P2822 组合数问题
Noip2016提高组day2 T1   题目描述   组合数表示的是从n个物品中选出m个物品的方案数。举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法。
1028 0
数论 + 公式 - HDU 4335 What is N?
What is N?  Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=4335   Mean:  给你三个数b、P、M,让你求有多少个n满足下式。
862 0
|
机器学习/深度学习
LCM性质 + 组合数 - HDU 5407 CRB and Candies
CRB and Candies Problem's Link  Mean:  给定一个数n,求LCM(C(n,0),C(n,1),C(n,2)...C(n,n))的值,(n
1016 0
|
Java Go
组合数学 - 母函数的运用 --- 模板题
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15064    Accepted ...
1117 0
poj 2528 Mayor&#39;s posters(线段树+离散化)
1 /* 2 poj 2528 Mayor's posters 3 线段树 + 离散化 4 5 离散化的理解: 6 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 7 会导致内存的耗尽。
898 0