Spreading the Wealth
题目
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.
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.
Output
For each input, output the minimum number of coins that must be transferred on a single line.
Simple Input
3
100
100
100
4
1
2
5
4
Simple Output
0 4
一句话题意
有n个人围圆桌而坐,每个人有Ai个金币,可以给左右相邻的人一些金币,使得最终所有人金币数相等,求最小金币转移数。
大致思路
对于任意的人i,他所有的关系只有两种,给i-1金币,从i+1得金币,设每个人最终应该有m个金币(金币数除以人数得m),m就可求。
根据上述所说,可以看做一个数列一样的东西,但并不是。可以用给第一个人的金币来表示所有人的,即如下所示:
x2=x1-A[i]+m;
x3=x2-A2+m=x1-A[1]-A[2]+2m;
x4=x3-A3+m=x1-A[1]-A[2]-A[3]+3m;
···
利用一个数组简化一下上边的式子:
令x2=x1-c[1];
x3=x2-c[2];
然后就能推出c[i]=c[i-1]+a[i]-m
然后利用这个,找到一个x1使所有c数组里的数值与x1做差所得的和最小,所以就可以推出c数组中数据的中位数是答案。
代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath> using namespace std;
typedef long long ll;
const int maxn = 1e6 + ;
ll A[maxn];
ll C[maxn];
ll n;
ll sum;
ll m; int main(){
while (~scanf("%d",&n)){
memset(A, , sizeof(A));
memset(C, , sizeof(C));
sum = ;
for (int i = ; i <= n; i++){
scanf("%d",&A[i]);
sum += A[i];
}
m = sum / n;
for (int i = ; i < n; i++){
C[i] = C[i - ] + A[i] - m;
}
sort(C, C + n);
ll x1 = C[n / ], ans = ;
for (int i = ; i < n; i++){
ans += abs(x1 - C[i]);
}
printf("%d\n",ans);
}
return ;
}
Spreading the Wealth的更多相关文章
- UVa 11300 Spreading the Wealth(有钱同使)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- Uva11300 Spreading the Wealth
设第i个人需要给第i+1个人的金币数为xi(xi为负代表收到钱),列出一大堆方程. 设第i个人给第i-1个人的钱为xi(xi<0表示第i-1个人给第i个人钱).计算出最后每个人应该有的钱m,解方 ...
- UVA 11300 Spreading the Wealth (数学推导 中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- uva 11300 - Spreading the Wealth(数论)
题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...
- 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 (思维题 中位数模型)
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(数学题)
UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...
- [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]
Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...
随机推荐
- DOM表单,下拉菜单和表格
DOM访问表单控件的常用属性和方法如下: action 返回该表单的提交地址 elements 返回表单内全部表单控件所组成的数组,通过数组可以访问表单内的任何表单控件. length 返回表单内表单 ...
- Html超链接和表单
超链接 a href="链接地址" target="链接打开方式" title="当鼠标放在连接上显示的文字"></a>链 ...
- (Java实现) 均分纸牌
题目描述 有 N 堆纸牌,编号分别为 1,2,-, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸牌,只能移到编号为 2 ...
- java实现Prim算法
1 问题描述 何为Prim算法? 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex ...
- java实现第N个素数
素数就是不能再进行等分的整数.比如:7,11.而9不是素数,因为它可以平分为3等份.一般认为最小的素数是2,接着是3,5,... 请问,第100002(十万零二)个素数是多少? 请注意:2 是第一素数 ...
- Markdown入门学习202004
Markdown入门学习202004 推荐使用Typora这款轻量级markdown编辑软件 标题 # 一级标题(井号后面有空格) ## 二级标题 ### 三级标题 ...... ###### 最多到 ...
- java 中有几种类型的流?
字节流,字符流. 字节流继承于 InputStream \ OutputStream, 字符流继承于 InputStreamReader \ OutputStreamWriter. 在 java.io ...
- throws,throw,try,catch,finally 分别代表什么 意义?
Java通过面向对象的方法进行异常处理,把各种不同的异常进行分类,并提供了良好的接口. 在Java中,每个异常都是一个对象,它是Throwable类或其它子类的实例.当一个方法出现异常后便 抛出一个异 ...
- centos7上安装memcached以及PHP安装memcached扩展(一)
安装memecached 第一步:安装libevent # tar zvxf libevent-2.1.8-stable.tar.gz # cd libevent-2.1.8-stable # ./c ...
- [转] Linux下用文件IO的方式操作GPIO(/sys/class/gpio)
点击阅读原文 一.概述 通过 sysfs 方式控制 GPIO,先访问 /sys/class/gpio 目录,向 export 文件写入 GPIO 编号,使得该 GPIO 的操作接口从内核空间暴露到用户 ...