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

Problem setter: Josh Bao

题目大意:n人围绕圆桌,每个人有一定数量金币,金币总数能被n整除。每个人可以给他左右的人一些金币,最终使每个人金币数相等。求被转手金币最小值。

解题思路:

  1、列出方程组:

    设Ai为初始金币数,xi表示i号给i-1号金币数[可正可负],M为最终每个人的金币数,则:

      for the first person:  A1-x1+x2=M-->x2=M-A1+x1=x1-C1[规定C1=A1-M]

       for the second person:   A2-x2+x3=M-->x3=M-A2+x2=2M-A1-A2+x1=x1-C2

       for the third person:   A3-x3+x4=M-->x4=M-A3+x3=3M-A1-A2-A3+x1=x1-C3

       ......

       for the nth person:  An-xn+x1=M[这是一个多余的式子,并不能给我们更多的信息]

  2、转换为单变量问题:

    我们希望所有xi的绝对值之和最小,即:|x1|+|x1-C1|+|x1-C2|+...+|x1-Cn|最小。

  3、转换为几何问题:

    注意上式的几何意义就是数轴上给定n个点找出一个到他们之和尽量小的点。

  4、递归求最优解:

    这个最优的x1就是他们的中位数。

 #include<iostream>
#include<algorithm>
#include<cstdio>
typedef long long LL;
using namespace std;
LL A[],C[];//A[i]表示第i人初始金币数
int main(){
int n;
while(cin>>n){
LL tot=;//总数
for(int i=;i<n;i++){
cin>>A[i];
tot+=A[i];
}
LL M=tot/n;//平均数
C[]=;
for(int i=;i<n;i++){
C[i]=C[i-]+A[i]-M;//递推数组C
}
sort(C,C+n);
LL x1=C[n/],ans=;//计算x1
for(int i=;i<n;i++){
ans+=abs(x1-C[i]);
}
cout<<ans<<'\n';
}return ;
}

[ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]的更多相关文章

  1. UVa 11300 Spreading the Wealth 分金币

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. Math - Uva 11300 Spreading the Wealth

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

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

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

随机推荐

  1. docker中安装ssh服务

    系统:Debian Docker 目标:在docker(debian系统)中安装ssh服务,实现远程登陆和控制docker 步骤: 初始状态:通过docker pull debian得到的一个debi ...

  2. json 特殊字符 javascript 特殊字符处理(转载)

    特殊字符以前都是禁止页面输入,这样就简单不容易出错,但最近需求要求能输入特殊字符整理出java返回json时特殊字符的转义(不转义会破坏json数据格式导致页面读取数据出错) Java代码 publi ...

  3. MVC concept

    What are MVP and MVC and what is the difference? http://stackoverflow.com/questions/2056/what-are-mv ...

  4. C#的 构造函数 和 方法重载

    构造函数(一本正经的讲构造函数 如果想看不正经的往下翻看方法重载) 方法名称与类名相同,没有返回值类型,连void都没有 用作给类的对象初始化 一个类中可以有多个构造 如果手动添加一个构造,系统不会自 ...

  5. Python全栈---5.1---装饰器

    一.装饰器 执行outer函数,将index作为参数传递, 将outer函数的返回值,重新赋值给index 装饰器可以在函数执行前和执行后执行其他的附加功能,这种在代码运行期间动态增加功能的方式,称之 ...

  6. Python全栈之路8--迭代器(iter)和生成器(yield)

    一.生成器( iter ) 从Python2.2起,生成器提供了一种简洁的方式帮助返回列表元素的函数来完成简单和有效的代码. 它基于yield指令,允许停止函数并立即返回结果.此函数保存其执行上下文, ...

  7. 爬虫---request+++urllib

    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕 ...

  8. SeleniumIDE初级入门

    Selenium  IDE:它是使用Javascript 脚本语言与CS端的DOM对象进行交互,并且为修改测试用例提供了接口.录制功能是它最大的亮点,录制功能可以让测试人员对需要进行测试的功能流程进行 ...

  9. AS错误:Please configure your build for VectorDrawableCompat.

    运行第一个Android Stdio程序就出现下面这个问题,由于对Android Stdio不熟悉整了三个小时才解决,希望这篇博客能帮助更多的人. 问题:Caused by:java.lang.Run ...

  10. UVA 753 A Plug for UNIX(二分图匹配)

    A Plug for UNIX You are in charge of setting up the press room for the inaugural meeting of the Unit ...