题目

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]+3
m; 
···

利用一个数组简化一下上边的式子:
令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的更多相关文章

  1. UVa 11300 Spreading the Wealth(有钱同使)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...

  2. Uva11300 Spreading the Wealth

    设第i个人需要给第i+1个人的金币数为xi(xi为负代表收到钱),列出一大堆方程. 设第i个人给第i-1个人的钱为xi(xi<0表示第i-1个人给第i个人钱).计算出最后每个人应该有的钱m,解方 ...

  3. UVA 11300 Spreading the Wealth (数学推导 中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  4. uva 11300 - Spreading the Wealth(数论)

    题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...

  5. Uva 11300 Spreading the Wealth(递推,中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  6. Math - Uva 11300 Spreading the Wealth

    Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...

  7. UVA.11300 Spreading the Wealth (思维题 中位数模型)

    UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...

  8. 数学/思维 UVA 11300 Spreading the Wealth

    题目传送门 /* 假设x1为1号给n号的金币数(逆时针),下面类似 a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[ ...

  9. UVA - 11300 Spreading the Wealth(数学题)

    UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...

  10. [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]

    Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...

随机推荐

  1. EntityFramework数据持久化 Linq语法应用

    Linq基础语法 LINQ概述 LINQ(Language Integrated Query,语言集成查询)提供了一种跨数据源和数据格式查询的统一模型. LINQ的组成: LINQ To Object ...

  2. Java实现蓝桥杯VIP 算法训练 P0502

    试题 算法训练 P0502 资源限制 时间限制:1.0s 内存限制:256.0MB 编写一个程序,读入一组整数,这组整数是按照从小到大的顺序排列的,它们的个数N也是由用户输入的,最多不会超过20.然后 ...

  3. Java实现 LeetCode 209 长度最小的子数组

    209. 长度最小的子数组 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = ...

  4. Java实现第八届蓝桥杯青蛙跳杯子

    青蛙跳杯子 题目描述 X星球的流行宠物是青蛙,一般有两种颜色:白色和黑色. X星球的居民喜欢把它们放在一排茶杯里,这样可以观察它们跳来跳去. 如下图,有一排杯子,左边的一个是空着的,右边的杯子,每个里 ...

  5. python自学Day03(自学书籍python编程从入门到实践)

    第4章 操作列表 只需要几行代码无论列表有多长,循环都能够让我对列表的每个元素都采取一个或一系列相同的措施,从而高效的处理任何长度的列表. 4.1 遍历整个列表 对列表中每个元素都拿出来,进行一个或者 ...

  6. 2.keras-构建基本网络实现非线性回归

    构建基本网络实现非线性回归 1.加载显示数据集 import tensorflow as tf import numpy as np import keras from keras.layers im ...

  7. tensorflow2.0学习笔记第二章第二节

    2.2复杂度和学习率 指数衰减学习率可以先用较大的学习率,快速得到较优解,然后逐步减少学习率,使得模型在训练后期稳定指数衰减学习率 = 初始学习率 * 学习率衰减率^(当前轮数/多少轮衰减一次) 空间 ...

  8. iOS-自定义Model转场动画-仿酷我音乐播放器效果

    周末,闲来无事,仿写了酷我音乐播放器效果: 效果图如下: 实现思路: 1.实现手势处理视图旋转 2.自定义Model动画: 1.手势是利用了一个UIPanGestureRecognizer手势: 注意 ...

  9. Layui 实现一个高级筛选功能

    基于layui写的一个高级搜索(筛选)功能.效果图: 是一位萌新,所有写的有点儿乱.(放在上面,供新手们参考,也是自己做一个记录.)代码如下: <!DOCTYPE html PUBLIC &qu ...

  10. 处理npm安装模块报错01

    报错:Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/nodemon_tmp' 解决:sudo cnpm in ...