[ACM_几何] UVA 11300 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
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 [分金币 左右给 最终相等 方程组 中位数]的更多相关文章
- UVa 11300 Spreading the Wealth 分金币
圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值,比如 n = 4, ...
- 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 (思维题 中位数模型)
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(递推,中位数)
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 ---------------------------------------------------------------- ...
- UVA 11300 Spreading the Wealth (数学推导 中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
随机推荐
- IE6,IE7上设置body{overflow:hidden;}失效Bug
IE6,IE7下设置body{overflow:hidden;}失效Bug 最近做项目发现在IE7下设置body{overflow:hidden;}后还是会出现纵向滚动条,所以上网查查了,在这里记录一 ...
- Installshield 打包安装包心得
制作简单的安装软件 声明:下面的教程,是把读者当做完全没接触过IS的角度来制作的. 1. 启动InstallShield 12.建立一个InstallShield MSI Project,如图: 2 ...
- args
java 中args一般存在main主类方法内,String args[ ]或者String[ ] args表示给主方法传一个字符串数组. 而args是一个字符串数组的变量名,不是关键字,是argum ...
- Nginx配置配置文件nginx.conf的设置
引用自:http://www.ha97.com/5194.html #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_pro ...
- eclipse的ssh框架详解
1.创建项目 2.导包 1.1:导入Struts2可能用到的包: 先从网站下载 再这里找出,打开它把WEB-INF/lib/下的所有包导入项目 1.2:导入spring可能用到的包: 先从网站下载 ...
- oracle数据学习第二天
今天主要加强了对oracle数据库的数据类型一些相关函数的学习 (一)char和varchar2 字符串函数 <1>concat(char1,char2)字符串连接函数,用于连接两个字 ...
- linux ddos防御攻击
Linux Ddos防御攻击 [root@lxh ~]# netstat -ntu |awk '{print $5}'|grep '[0-9]'|cut -d: -f1 |sort |uniq -c| ...
- 存在网路的情况下重命名SDE中数据图层错误(The orphan junction feature class cannot be renamed)
运行环境为ArcGIS9.3,VS2008. 问题描述:数据通过SDE存储在Oracle10g数据库中,数据集中存在几何网络,在存在网络的情况下通过程序对其中的数据图层进行重命名,弹出"Th ...
- Oracle 索引<七>
Oracle 索引 管理索引-原理介绍 介绍 索引是用于加速数据存取的数据对象.合理的使用索引可以大大降低 i/o 次数,从而提高数据访问性能.索引有很多种我们主要介绍常用的几种: 为什么添加了索 ...
- [MongoDB]Mongodb攻略
-------------------------------------------------------------------------------------------- [基础] 1. ...