算法竞赛入门经典训练指南——UVA 11300 preading 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.
Sample Input
3
100
100
100
4
1
2
5
4
Sample Output
0
4
算法竞赛入门经典训练指南上有题意与分析,不懂的可以去翻阅。
//Asimple
#include <bits/stdc++.h>
#define INF (1<<20)
#define mod 10007
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
using namespace std;
typedef long long ll;
const int maxn = ;
ll n, m, res, ans, len, T, k, num, sum;
ll x, y;
ll a[maxn], c[maxn]; /*
假设 An表示n个人原有的金币数
xn表示第n个人给n-1个人xn枚金币(x1表示1给n)
M表示最终每个人拥有的金币数
则可推出通式: xn = x1-Cn 其中 Cn = A1+A2+...+An-n*M
由于要求是最小的金币数,所以x1应当取C数组的中位数
*/ void input() {
ios_base::sync_with_stdio(false);
while( cin >> n ) {
sum = ;
for(int i=; i<=n; i++) {
cin >> a[i];
sum += a[i];
}
ll M = sum / n;
c[] = ;
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]);
cout << ans << endl;
}
} int main(){
input();
return ;
}
算法竞赛入门经典训练指南——UVA 11300 preading the Wealth的更多相关文章
- 算法竞赛入门经典-训练指南(10881-Piotr's Ants)
题目大意: 一根长度为L的木棍一堆蚂蚁爬,向左或向右,速度都为1,若两蚂蚁碰撞则同时转头(转身时间忽略不计),问T时间之后每只蚂蚁的位置: 输入:t,(t个样例),每个样例输入 L,T,n,接下来是n ...
- 算法竞赛入门经典 LA 4329(树状数组)
题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- [刷题]算法竞赛入门经典 3-12/UVa11809
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...
- [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...
- [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...
随机推荐
- [硬件]SICK LMS111激光扫描仪使用
1.电源接入 电源线5Pin,4芯.棕色线接入正极,蓝色线接入负极(直流电,地线).LMS111-100默认的电压范围是10-30V,推荐使用24V,我这里使用的是12V/3A的锂电池. 另外两根白色 ...
- swiper 视频轮番
百度搜索:swiper 视频轮番 转载1:https://blog.csdn.net/Aimee1608/article/details/79637929 项目中使用swiper插件嵌套video标签 ...
- sendmail 发送邮件 zabbix 自定义报警
配合zabbix 触发脚本 达到自定义报警目的 #!/bin/bash # Created : 2015.12.08 # Updated : 2015.12.08 # Author : sanmuya ...
- 【LeetCode每天一题】Search Insert Position(搜索查找位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- js模拟队列----小优先队列
队列:先进先出,后进后出 var Queue = (function(){ var item = new WeakMap(); class Queue{ constructor(){ item.set ...
- chrome debug 服务端性能
设置 http header 在 chrome 查看服务端性能 \Yii::$app->getResponse()->headers->set('Server-Timing', 'c ...
- .yml文件格式
http://yaml.org/ YAML: YAML Ain't Markup Language What It Is: YAML is a human friendly data serializ ...
- python SMTP
一.一开始,相信SMTP服务,所以在本机安装了一个 apt-get install sendmail apt-get install sendmail-cf apt-get install squir ...
- QT linux
一. 如果你是服务器版的CentOS,我建议你安装一下图形界面 1. 首先安装X window: yum groupinstall 'X WindowSystem'2. 然后安装GNOME: yum ...
- Cocos Creator 动作(动画)笔记
动作cc.ActionInterval 和cc.ActionInstant; var action = cc.moveTo(2, 100, 100); // 创建一个移动动作node.runActio ...