UVa 11300 - Spreading the Wealth(有钱同使)

Time limit: 6.000 seconds


Description - 题目描述

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.

某村子准备重新分配财富进一步迈向共产主义。他们已召集好全部人等围坐在圆桌前。首先,每个人都把其全部财富转换为若干等价的硬币。然后将硬币总数除以村子人数。最后,每个人可以分别给予左右两人若干硬币,使得所有人的硬币数量相同。给你每个人的初始硬币数量,计算最少需要转移多少枚次(比对“人次”)硬币才能按上述方法完成最终分配。

CN

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.

多组测试用例。每组以n(n < )打头,表示这个村子的总人数。随后n行以逆时针顺序给出桌上每个人的硬币数量。硬币的总数少于无符号64位整数。(然而实际情况似乎用有符号64位就能解决)

CN

Output - 输出

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

对于每组测试用例,输出一行最少需要转移的硬币枚次。

CN

Sample Input - 输入样例

3
100
100
100
4
1
2
5
4

Sample Output - 输出样例

0
4

题解

绝对值不等式的题目,选好推倒的关系很快就能推出来。题目似乎在数据放水了,没有出到unsigned 64。

设:

  第i个人给下一个人的硬币为Ci(其值可正可负)

  第i个人初始硬币为Pi,第1~i个人的硬币之和为Spi

  平均数为A

则:

P1 + Cn - C1 = A

P2 + C1 - C2 = A

P3 + C2 - C3 = A

................

Pn + Cn-1 - Cn = A

最终需要求的流动硬币的数量S = |C1| + |C2| + |C3| + ... + |Cn|

因此后面开始把C1 ~ Cn 化简出来:

      C1 = P1 - A + Cn = Sp1 - A + Cn

      C2 = P2 - A + C1 = Sp1 + P2 - 2A + Cn = Sp2 - 2A + Cn

      C3 = P3 - A + C2 = Sp2 + P3 - 3A + Cn = Sp3 - 3A + Cn

      C4 = P4 - A + C3 = Sp3 + P4 - 4A + Cn = Sp4 - 4A + Cn

      ......................

      Cn = Spn - nA + Cn

此时已经找出递推式了,后面为了说明简单,令Cpn = Spn - nA

      S = |C1| + |C2| + |C3| + ... + |Cn|

      S = |Cp1+Cn| + |Cp2+Cn| + |Cp3+Cn| + ... |Cpn+Cn|

最后一个|Cpn+Cn|已经是0了,可以忽略掉:)

得到了S的表达式,我们要做的就是求最小值,由于S >= 0,很容易想到绝对值不等式。

|Cp1+Cn| + |Cp2+Cn| + |Cp3+Cn| + ... |Cpn+Cn| >= 0

如果取最小值,则 Cn = -(Cp1 ~ Cpn的中位数)。(n为偶数时靠左靠右无所谓,画个图就知道了:))

奇数

偶数

代码 C++

 #include <cstdio>
#include <algorithm>
long long Sp[], Cp[];
int main(){
int n, i;
long long opt, Avg, Cn;
while (~scanf("%d", &n)){
for (i = ; i <= n; ++i){
scanf("%lld", &Sp[i]); Sp[i] += Sp[i - ];
}
Avg = Sp[--i] / n;
for (i = ; i <= n; ++i) Cp[i] = Sp[i] - Avg*i;
std::sort(Cp + , Cp + + n); opt = ;
Cn = -Cp[ + n >> ];
for (i = ; i <= n; ++i){
if ((Cp[i] += Cn) < ) Cp[i] = -Cp[i];
opt += Cp[i];
}
printf("%lld\n", opt);
}
return ;
}

UVa 11300 Spreading the Wealth(有钱同使)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. Math - Uva 11300 Spreading the Wealth

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

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

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

  9. UVA 11300 Spreading the Wealth

    题目大意:n个人手中有些金币,每个人可给相邻两个人一些金币,使得最终每个人手中金币数相同,求被转手的金币最少数 m为最终每个人手中的金币数,a1,a2,a3,...,an为每个人开始时手中的金币数,x ...

随机推荐

  1. linux 解压

    .tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...

  2. web.xml配置error-page

    一. 通过错误码来配置error-page <error-page> <error-code>404</error-code> <location>/e ...

  3. 我自己的Javascript 库,封装了一些常用函数 Kingwell.js

    我自己的Javascript 库,封装了一些常用函数 Kingwell.js 博客分类: Javascript javascript 库javascript库  现在Javascript库海量,流行的 ...

  4. 【java基础学习】线程

    线程 1. 两种创建方式(继承Thread类和实现Runnable接口) 2. 线程共享资源(建议实现Runnable接口,其好处是:1.多线程之间可以共享资源 2.避免单继承带来的问题 3.数据和代 ...

  5. [SLAM] GMapping SLAM源码阅读(草稿)

    目前可以从很多地方得到RBPF的代码,主要看的是Cyrill Stachniss的代码,据此进行理解. Author:Giorgio Grisetti; Cyrill Stachniss  http: ...

  6. (转载)throw和throws的区别

    1.throw:(针对对象的做法)抛出一个异常,可以是系统定义的,也可以是自己定义的.下面举两个例子:抛出Java中的一个系统异常:public class One {public void yich ...

  7. HTTP协议上传boundary确定&下载content-disposition理解

    HTTP协议上传文件-协议 上传文件需要将form标签 的 ENCTYPE 属性设置为 multipart/form-data属性, 与 application/x-www-form-urlencod ...

  8. VMware下安装虚拟机Ubuntu14.04 Server设置桥接方式

    我本地的采用的上网方式的拨号上网,IP段是一公网下的通过路由设置的局域网,网段182.18.1.* 本地连接包含以下: 其中无线上网卡的.WMware桥接是自定义的局域网IP段:192.168.253 ...

  9. openssl 证书操作命令

    生成Self Signed证书 # 生成一个key,你的私钥,openssl会提示你输入一个密码,可以输入,也可以不输, # 输入的话,以后每次使用这个key的时候都要输入密码,安全起见,还是应该有一 ...

  10. curl常用选项详解

    curl常用选项详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 又是下班的时间了,让我们一起来学习一下今天的Linux命令吧~我一半只把自己常用的参数列出来,其他的有但是我们几 ...