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. Oracle数据库部分迁至闪存存储方案

    Oracle数据库部分迁至闪存存储方案 1.实施需求 2.确认迁移表空间信息 3.确认redo信息 4.确认undo信息 5.表空间迁移到闪存 6.redo迁移到闪存 7.undo迁移到闪存 8.备库 ...

  2. 命令行执行php脚本 中$argv和$argc

    在实际工作中有可能会碰到需要在nginx命令行执行php脚本的时候,当然你可以去配置一个conf用外网访问. 在nginx命令行中 使用 php index.php 就可以执行这个index.php脚 ...

  3. python模拟shell执行脚本

    工作时候需要模拟shell来执行任务,借助包paramkio import paramiko class ShellExec(object): host = '127.0.0.1' port = 36 ...

  4. bzoj 3932: [CQOI2015]任务查询系统

    Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...

  5. tcpdump 命令详解

    TCPdump: dump  traffic on a network ,根据使用者的定义对网络上的数据包进行截获的一种包分析工具. TCPdump 可以讲网络中传送的数据包的 头信息完全截获下来提供 ...

  6. Linq To EF

    l简单查询:var result = from c in Entities.Customer select c; l条件查询: 普通linq写法: var result = from c in Ent ...

  7. Mac_OS_Sierra_10.12.6编译OpenJDK9

    编译环境以及依赖 macOS:Sierra,10.12.6 处理器:2.6 GHz Intel Core i7 内存:16 GB 2133 MHz LPDDR3 Command Line Tools ...

  8. 自动生成getter,setter方法的插件lombok

    1.在InteiliJ IDEA上安装lombok插件,并重启 . 2.在pom.xml文件中添加依赖 <dependency>    <groupId>org.project ...

  9. 微信小程序部署问题总结

    1.微信小程序免费SSL证书Https 申请(阿里云申请) 进入阿里云控制台后,选择CA证书服务 选择购买证书 但是阿里云的免费SSL证书藏得比较深,得这样操作才能显示出免费证书 点击Symantec ...

  10. (转)top关键字与top表达式(SQLServer)

    SQLServer 中,top也很有用,例如查询部分数据,还可以用表达式.其语法如下: SELECT TOP number|percent column_name(s) FROM table_name ...