题目

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. Python机器学习笔记:SVM(1)——SVM概述

    前言 整理SVM(support vector machine)的笔记是一个非常麻烦的事情,一方面这个东西本来就不好理解,要深入学习需要花费大量的时间和精力,另一方面我本身也是个初学者,整理起来难免思 ...

  2. 从0开始探究vue-双向绑定原理

    理解 vue是一个非常优秀的框架,其优秀的双向绑定原理,mvvm模型,组件,路由解析器等,非常的灵活方便,也使开发者能够着重于数据处理,让开发者更清晰的设计自己的业务. 双向绑定,就是数据变化的时候, ...

  3. 高性能可扩展mysql 笔记(五)商品实体、订单实体、DB规划

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.商品模块 ​ 商品实体信息所存储的表包括: 品牌信息表: create table `brand_i ...

  4. ASP.NET防止自己网站的资源被盗(通过IHttpHandler 带样例说明)

    我这里用的图片被盗举例子 一个正常的网页 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind ...

  5. (Java实现) 洛谷 P1691 有重复元素的排列问题

    题目描述 设R={r1,r2,--,rn}是要进行排列的n个元素.其中元素r1,r2,--,rn可能相同.使设计一个算法,列出R的所有不同排列. 给定n以及待排列的n个元素.计算出这n个元素的所有不同 ...

  6. Java实现提取拼音首字母

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

  7. 第03组 Alpha(2/4)

    队名:不等式方程组 组长博客 作业博客 团队项目进度 组员一:张逸杰(组长) 过去两天完成的任务: 文字/口头描述: 制定了初步的项目计划,并开始学习一些推荐.搜索类算法 GitHub签入纪录: 暂无 ...

  8. iOS-PCH File的快速导入方法和使用

    PCH的文件的用途:      在实际的项目开发中,如果很多地方都在使用某个类的头文件,很多地方都在使用同一个”宏”的时候:很多地方用到了NSLog()函数, 在app发布的时候,想清除掉时,此时就需 ...

  9. 00-01.Kali Linux 2020.1修改root用户密码

    安装Kali Linux 2020.1系统后,需要使用root用户权限安装软件. 由于VMWare版本的root用户默认密码未知,所以需要在单用户模式下重新设置root用户密码.操作步骤如下: 启动K ...

  10. SSM-maven 简单整合

    记一次简单的整合了一次SSM框架过程,因为好久不用了在过程中发生了一些问题. 后续我会在继续在此基础上整合其他框架进来. 本次整合用到的框架有: spring & springmvc myba ...