算法竞赛入门经典训练指南——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 ...
随机推荐
- su - 和su的区别
su root和su - root: su只是切换了root身份,但Shell环境仍然是普通用户的Shell:而su -连用户和Shell环境一起切换成root身份了 推荐使用su - .
- PHPUnit单元测试的简单使用
何为单元测试: 指对软件中的基本单元进行测试,如函数.方法等,以检查其返回值或行为是否符合预期:实际中软件是很复杂的,由许多组件构成,执行流程连贯在一起,要进行单元片段的测试,就需要为其提供执行上下文 ...
- [js]promise学习2
let fs = require("fs"), path = require('path'); 普通读取文件方法 /* fs.readFile(path.resolve('./da ...
- Mybatis中映射器实现方式总结
一种是通过XML文件方式(由一个java接口和一个XML文件构成) RoleMapper rm = session.getMapper(RoleMapper.class); List<Role& ...
- composer install 遭遇404错误
[Composer\Downloader\TransportException] The "https://packagist.phpcomposer.com/p/provider-2019 ...
- CentOS6.5 安装openssl
安装比较简单! 1.下载安装包 [root@mycentos ~]# wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz 2.解压和编译 ...
- Kotlin sealed class
密封类的概念对于我这种从古代语言进化到现代语言的老古董来说还是有点绕腾的啊! 1. 密封类用来表示受限的类继承结构 解释:类中 元素值限制在某一个集合之中 2. 密封类可以有子类,但是所有的子类都必须 ...
- android hook native函数
大概2年前写的代码,今天突然要用到,找了半天,这里记录下 用到的库: https://pan.baidu.com/s/1htuUQX2 #include <jni.h> #include ...
- export,import ,export default 彻底弄痛
ES6模块主要有两个功能:export和import 说白了就是一个淡出一个导入,就相当于以前的公共js样,哪个页面要用,就script 引入这个js ,然后 无耻的调用这个js中的方法了. ex ...
- 从零开始一起学习SLAM | 相机成像模型
上一篇文章<从零开始一起学习SLAM | 为啥需要李群与李代数?>以小白和师兄的对话展开,受到了很多读者的好评.本文继续采用对话的方式来学习一下相机成像模型,这个是SLAM中极其重要的内容 ...