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代码:

 #include <bits/stdc++.h>
using namespace std;
const int maxn=;
long long A[maxn],C[maxn],tot,M;
int main()
{
int n;
while(scanf("%d",&n)==)//这里写EOF会超时,我也不知道咋回事
{
tot=;
for(int i=;i<=n;i++)
{
scanf("%lld",&A[i]);
tot+=A[i];
}
M=tot/n;//求平均值
C[]=;
for(int i=;i<n;i++)
C[i]=C[i-]+A[i]-M;//有个递推公式,数学的博大精深啊!
sort(C,C+n);
long long x1=C[n/];
long long ans=;
for(int i=;i<n;i++)
ans+=abs(x1-C[i]);
printf("%lld\n",ans);
}
return ;
}

Uva 11300 Spreading the Wealth(递推,中位数)的更多相关文章

  1. UVA.11300 Spreading the Wealth (思维题 中位数模型)

    UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...

  2. UVa 11300 Spreading the Wealth(有钱同使)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...

  3. uva 11300 - Spreading the Wealth(数论)

    题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...

  4. 数学/思维 UVA 11300 Spreading the Wealth

    题目传送门 /* 假设x1为1号给n号的金币数(逆时针),下面类似 a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[ ...

  5. UVA - 11300 Spreading the Wealth(数学题)

    UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...

  6. UVA 11300 Spreading the Wealth (数学推导 中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  7. [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]

    Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...

  8. Math - Uva 11300 Spreading the Wealth

    Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...

  9. UVa 11300 Spreading the Wealth 分金币

    圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值,比如 n = 4, ...

随机推荐

  1. 小白的Python之路 day2 字符编码和转码

    字符编码和转码 详细文章: http://www.cnblogs.com/yuanchenqi/articles/5956943.html http://www.diveintopython3.net ...

  2. SoapUI模拟soap接口返回不同响应(通过groovy脚本)

    一.创建soap项目,输入wsdl文件,然后生成SOAP Mock Service,再生成测试用例,然后新建新的响应 WSDL文件:MathUtil.wsdl <?xml version=&qu ...

  3. 【ANT】description元素和属性

    <?xml version="1.0" ?> <project default="test"> <description> ...

  4. PDO绑定含IN的SQL语句的参数注意事项

    PDOStatement::bindParam(),表示将PDO::prepare()语句中的占位符用参数替换掉.注意,在绑定含有IN的SQL多参数语句时要额外小心,比如$stmt = $db-> ...

  5. geoserver安装部署步骤

    方式一:直接在geoserver官网下载zip源代码解压包,直接部署在tomcat里面运行geoserver: 方式二:下载安装包方式 以GeoServer2.8.5版本为准,安装之前必须要保证你机子 ...

  6. Java面试题汇总

    第一阶段:三年我认为三年对于程序员来说是第一个门槛,这个阶段将会淘汰掉一批不适合写代码的人.这一阶段,我们走出校园,迈入社会,成为一名程序员,正式从书本 上的内容迈向真正的企业级开发.我们知道如何团队 ...

  7. js跳转页面的几种方式

    第一种: window.location.href="http://www.baidu.com"; 第二种: window.history.back(-1); 第三种: windo ...

  8. 《精通Spring 4.X企业应用开发实战》读书笔记1-1(IoC容器和Bean)

    很长一段时间关注在Java Web开发的方向上,提及到Jave Web开发就绕不开Spring全家桶系列,使用面向百度,谷歌的编程方法能够完成大部分的工作.但是这种不系统的了解总觉得自己的知识有所欠缺 ...

  9. Mysql使用alias 防止对数据的误操作

    在我们操作数据库的时候,尤其是执行,update,delete操作的时候,都存在着误操作的风险,今天发现一种方法,能避免这一问题,就是使用Mysql的alias . 1.查看Mysql帮助 #mysq ...

  10. Django内置Admin

    Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: 依赖APP: django.contrib.auth django.contrib.contenttyp ...