题目

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. (二)用less+gulp+requireJs 搭建项目(gulp)

    gulp是自动化构建工具,基于node,需要安装node,如果你不了解node也没关系,先跟着来一遍再去了解node也不迟~ 首先去node官网下载安装包 1.新建项目文件夹 在目录下shift+右键 ...

  2. Java实现 蓝桥杯VIP 算法提高 笨小猴

    算法提高 笨小猴 时间限制:1.0s 内存限制:256.0MB 问题描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率 ...

  3. Java实现 LeetCode 75 颜色分类

    75. 颜色分类 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红 ...

  4. Java实现 洛谷 P1598 垂直柱状图

    题目描述 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过100个字符),然后用柱状图输出每个字符在输入文件中出现的次数.严格地按照输出样例来安排你的输出格式. 输入格式 四行字符, ...

  5. java实现第三届蓝桥杯拼音字母

    拼音字母 在很多软件中,输入拼音的首写字母就可以快速定位到某个词条.比如,在铁路售票软件中,输入: "bj"就可以定位到"北京".怎样在自己的软件中实现这个功能 ...

  6. shell中文本内容多行变一行的技巧

    在linux下有时可能需要将多行的值转成一行.其实现的方法有很多种.笔者将自己曾经用过的方法在些分享. 如有一文本文件5201351.txt,文本的内容如下: 现我们可以通过如下方法将文本内容转成一行 ...

  7. BigDecimal的setScale常用方法(ROUND_UP、ROUND_DOWN、ROUND_HALF_UP、ROUND_HALF_DOWN)

    BigDecimal的setScale四大常用方法总结 // 设置小数点后第三位数字一大一小观察效果BigDecimal num = new BigDecimal("3.3235667&qu ...

  8. 曹工说JDK源码(3)--ConcurrentHashMap,Hash算法优化、位运算揭秘

    hashcode,有点讲究 什么是好的hashcode,一般来说,一个hashcode,一般用int来表示,32位. 下面两个hashcode,大家觉得怎么样? 0111 1111 1111 1111 ...

  9. 实验四 Linux系统搭建C语言编程环境

    项目 内容 这个作业属于那个课程 <班级课程的主页链接> 这个作业的要求在哪里 <作业要求链接地址> 学号-姓名 17043220-万文文 作业学习目标 1).Linux系统下 ...

  10. BT.656视频信号解码

    BT.656视频信号解码   BT.656协议标准 ITU-R BT.601和ITU-R BT.656是ITU-R(国际电信联盟)制定的标准.严格来说ITU-R BT.656是ITU-R BT.601 ...