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. 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
题目大意:圆桌旁边坐着n个人,每人有一定数量的金币,金币总数能被n整除。每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等。你的任务是求出被转手的金币数量的最小值。比如n=4,且4个人的金币数量分别为1,2,5,4时,只需转移4枚金币(第3个人给第2个人2枚金币,第2个人和第4个人分别给第1个人1枚金币)即可实现每人手中的金币数目相等。
分析:首先最终每个人的金币数量可以计算出来,它等于金币总数除以人数n。接下来用M来表示每个人最终拥有的金币数。
现在假设编号为 i 的人初始有Ai 枚金币,对于1号来说,他给了4号x1枚金币,还剩Ai -x1枚金币;但是2号给了他x2枚金币,所以还剩A1-x1+x2枚金币。所以A1-x1+x2=M。同理对于第2个人,有A2-x2+x3=M。最终得到n个方程,实际上只有n-1个有用。
尝试用x1表示出其他的xi ,则本题就变成了单变量的极值问题。
对于第1个人,A1-x1+x2=M →x2=M-A1+x1=x1-C1(规定C1=A1-M,下面类似)
对于第2个人,A2-x2+x3=M →x3=M-A2+x2=2M-A1-A2+x1=x1-C2
对于第3个人,A3-x3+x4=M →x4=M-A3+x3=3M-A1-A2-A3+x1=x1-C3
...
对于第n个人,An-xn+x1=M。这是一个多余的等式。
我们希望所有xi 的绝对值之和尽量小,即|x1|+|x1-C1|+|x1-C2|+...+|x1-Cn-1|要最小。注意到|x1-Ci|的几何意义是数轴上的点x1到Ci 的距离,所以问题变成了:给定数轴上n个点,找出一个到他们距离之和尽量小的点。
而这个点就是中位数,它实在是太优美,太巧妙了,而且不少其他问题也能用的上,接下来要证明它。
证明:给定数轴上的n个点,在数轴上的所有点中,中位数离所有顶点的距离之和最小。
在有6个点的数轴上,任取一点,比如最左边和最右边点的中点K,它的左边有4个点,右边有2个点。把它往左移一点,不要移太多,以免碰到输入点。假设移动的d单位距离,则这个点K左边4个点到它的距离各减少了d,右边的2个点到它的距离各增加了d,但总的来说,距离之和减少了2d。如果K的左边有2个点,右边有4个点,道理类似,不过应该向右移动。
换句话说,只要灰点左右的输入点不一样多,就不是最优解。所以,如果输入点一共有奇数个,则灰点必须和中间的那个点重合(中位数);如果有偶数个,则灰点可以位于最中间两个点之间的任意位置(还是中位数)。
代码如下:
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn = + ;
long long A[maxn], C[maxn], tot, M;
int main() {
int n;
while(scanf("%d", &n) == ) { // 输入数据大,scanf比cin快
tot = ;
for(int i = ; i <= n; i++) { scanf("%lld", &A[i]); tot += A[i]; } // 用%lld输入long long
M = tot / n;
C[] = ;
for(int i = ; i < n; i++) C[i] = C[i-] + A[i] - M; // 递推C数组
sort(C, C+n);
long long x1 = C[n/], ans = ; // 计算x1
for(int i = ; i < n; i++) ans += abs(x1 - C[i]);
// 把x1代入,计算转手的总金币数
printf("%lld\n", ans);
}
return ;
}
另外%lld这个占位符不是跨平台的。虽然cin/cout没有这个问题,但是本题输入量比较大,cin/cout会比较慢。
UVA 11300 Spreading the Wealth (数学推导 中位数)的更多相关文章
- UVA.11300 Spreading the Wealth (思维题 中位数模型)
UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...
- 数学/思维 UVA 11300 Spreading the Wealth
题目传送门 /* 假设x1为1号给n号的金币数(逆时针),下面类似 a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[ ...
- UVA - 11300 Spreading the Wealth(数学题)
UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...
- UVa 11300 Spreading the Wealth(有钱同使)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- uva 11300 - Spreading the Wealth(数论)
题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...
- Uva 11300 Spreading the Wealth(递推,中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- Math - Uva 11300 Spreading the Wealth
Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...
- [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]
Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...
- uva 11300 Spreading the Wealth_数学推倒 + 思维
这道题和负载平衡问题是同一道题, 如果 n<=100n <= 100n<=100 的话是可以用最小费用流来求解的. 但是题中 nnn 最大可达到 10610^6106, 这就需要我们 ...
随机推荐
- C语言结构
一个进程在内存中的布局如图所示: .text段(正文段)——保存程序所执行的程序二进制文件,CPU执行的机器指令部分:一个程序只有一个副本:只读,防止程序由于意外事故而修改自身指令. . ...
- hdu 01背包汇总(1171+2546+1864+2955。。。
1171 题意比较简单,这道题比较特别的地方是01背包中,每个物体有一个价值有一个重量,比较价值最大,重量受限,这道题是价值受限情况下最大,也就值把01背包中的重量也改成价值. //Problem : ...
- Redis+Spring缓存实例(windows环境,附实例源码及详解)
原文出处: 小宝鸽 一.Redis了解 1.1.Redis介绍: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). ...
- 10个强大的Apache开源模块
1.单点登录模块 LemonLDAP LemonLdap可以很棒地实现Apache的SSO功能,并且可以处理超过 20 万的用户请求.LemonLdap支持Java, PHP, .Net, Perl, ...
- jquery ajax return值不能取得的解决方案
jQuery ajax - ajax() 方法 http://www.w3school.com.cn/jquery/ajax_ajax.asp http://www.cnblogs.com/fqw19 ...
- 问题-[delphi2007、2010]无法二次启动,报EditorLineEnds.ttr被占用,进程一直有bds.exe?
问题现象:delphi2007.2010无法二次启动,报EditorLineEnds.ttr被占用,而且进程中一直有bds.exe的进程? 问题原因:问题处理:方法一:可能是系统更新的东东造在的.KB ...
- 推送通知/传感器/UIDynamic仿真(推送通知已适配iOS10)
推送通知/传感器/UIDynamic 一.推送通知 1.推送通知简介 什么是推送通知 此处的推送通知与NSNotification没有任何关系 可以理解为,向用户推送一条信息来通知用户某件事情 作用: ...
- 【javascript】复制到剪贴板功能(支持目前各种浏览器)
本demo支持各种浏览器复制,亲测可用(IE8,IE9,IE10,火狐,谷歌). 本demo中使用了ZeroClipboard(下载地址:https://github.com/zeroclipboar ...
- Yii2 TimestampBehavior行为
<?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * ...
- 四、XML映射配置文件
MyBatis的XML配置文件包含了影响MyBatis行为甚深的设置和属性信息.XML文档的高层级结构如下: ----configuration配置 --------properties属性 ---- ...