算法竞赛入门经典训练指南——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 ...
随机推荐
- vue中less的使用
1.安装:npm install less less-loader --save 2.修改webpack.config.js文件,配置loader加载依赖,让其支持外部的less,在原来的代码上添加 ...
- Number (int float bool complex)--》int 整型、二进制整型、八进制整型、十六进制整型
# ### Number (int float bool complex) # (1) int 整型 (正整数 0 负整数) intvar = 15 print(intvar) intvar = 0 ...
- Openvpn配置文件详解
一.vars配置文件 vars配置文件的主要内容如下: cat vars |grep -vE "^#|^$" KEY_DIR定义key生成的目录. KEY_SIZE定义生成私钥的大 ...
- 如何创建线程第一种继承Thread类
步骤 1:定义一个类 继承Thread类.2:重写Thread类的run方法.3:直接创建Thread的子类对象创建线程.4:调用start方法开启线程并调用线程的任务run方法执行.-------- ...
- 创建vue项目的时候遇到:PhantomJS not found on PATH
1.提示找不到PhantomJS需要进行下载,如果网速允许的话可以直接 npm install -g phantomjs 如果网速不给力的话,那就先进行淘宝镜像安装 npm install -g cn ...
- Laravel中路由怎么写(一)
1.路由基本使用示例 1.1 默认示例 Laravel中所有路由定义在/app/Http/routes.php文件中,该文件默认定义了应用的首页路由: Route::get('/', function ...
- Linux df命令详解
1.命令:df 2.命令功能:显示指定磁盘文件的可用空间. 3.命令参数: -a #全部文件系统列表 -h #方便阅读方式显示 -H #等于“-h”,但是计算式,1K=1000,而不是1K=1024 ...
- js获取微信code
function callback(result) { alert('cucess'); alert(result); //输出openid } function getQueryString(nam ...
- docker同步时区时间
在Docker容器创建好之后,可能会发现容器时间跟宿主机时间不一致,这就需要同步它们的时间,让容器时间跟宿主机时间保持一致.如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...
- 数据加密之DES加密
DES加密即使用DESCryptoServiceProvider加密.DESCryptoServiceProvider在命名空间下:System.Security.Cryptography; 对称加密 ...