UVa 11300 Spreading the Wealth

本文解决了一个共产主义村落中财富(金币)的平均分配问题。通过数学建模将问题转化为求解最小化金币移动次数,利用中位数的概念找到最优解,并提供了一段C++代码实现。
 F. 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. nlines 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

 


Problem setter: Josh Bao 

 

n个人围坐在一个圆桌上,每个人前面有Ai枚金币,每个人的金币可以移给他左右的两个人,问最少移动多少枚金币可以使每个人面前的金币数相等

首先,根据输入的数据很容易求出最终每个人面前的金币数,设为M(程序中用的变量为ave,M=(A1+A2+…+An)/n)

设第i个人移给第i-1个人的金币数为Xi(X1为第1个人移给第n个人的金币数,另外如果Xi<0,则表示是由第i-1个人向第i个人移动了-Xi枚金币)

可以得到下面这个等式:M=Ai-Xi+X(i+1)

                    变形得:X(i+1)=M-Ai+Xi

设Ci=A1+A2+…+Ai-i*M(写成递推形式为C1=A1-M,Ci=C(i-1)+Ai-M)

则可以得到如下一系列等式:

  X2=M-A1+X1=X1-C1

  X3=M-A2+X2=2*M-A2-A1+X1=X1-C2

  X4=M-A3+X3=3*M-A3-A2-A1+X1=X1-C3

  ……  ……

  Xn=X1-C(n-1)

原问题是求|X1|+|X2|+|X3|+…+|Xn|的最小值

   即求|X1|+|X1-C1|+|X1-C2|+…+|X1-C(n-1)|的最小值

我们知道|X1-Ci|在数轴上表示两点之间距离,因此此题最终转化为在数轴上求一个点X1,使其到点0,C1,C2,……,C(n-1)的距离之和最小

这里最优的X1就是这些数的”中位数”(这个结论的证明参见刘汝佳的《算法竞赛入门经典——训练指南》第6页)

这样这道题就做完了

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 long long c[1000050];
 8 
 9 int main()
10 {
11     int n;
12     unsigned long long total;
13 
14     while(scanf("%d",&n)==1)
15     {
16         total=0;
17         for(int i=0;i<n;i++)
18         {
19             scanf("%lld",&c[i]);
20             total+=c[i];
21         }
22         long long ave=total/n;
23         c[0]=ave-c[0];
24         for(int i=1;i<n;i++)
25             c[i]=c[i-1]+ave-c[i];
26         sort(c,c+n);
27         long long mid=c[n/2];
28         unsigned long long ans=0;
29         for(int i=0;i<n;i++)
30             ans+=(mid>c[i]?mid-c[i]:c[i]-mid);
31         printf("%llu\n",ans);
32     }
33 
34     return 0;
35 }
[C++]

 

转载于:https://www.cnblogs.com/lzj-0218/p/3537309.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值