题目大意

圆桌旁边坐着\(n\)个人,每个人有一定数量的金币,金币的总数能被\(n\)整除。每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数量相等。您的任务是求出被转手的金币的数量的最小值。

Solution

考虑一下这个东西怎么做,假设每一个人向周围都可以传递对吧,设\(x_i\)表示\(i\)向\(i-1\)传递的金币数量,那么\(x_1\)就是\(1\)向\(n\)传递的金币数量,那么接下来我们考虑这个东西本质是什么?

设每一个人一开始有\(a_i\)个金币,那么显然,\(a_i+x_{i+1}-x_i=M\)(M为\(sum/n\))。我们要最小化\(\sum_{i=0}^{n-1}|x_i|\)

我们把这个转换一下:

\(x_i=a_i+x_{i+1}-M\)

但是这个是不好转换的,所以考虑通过\(i\)算\(i+1\)的贡献。

\(x_{i+1}=M+x_i-a_i\)

所以进一步转换就是

\[x_i=M+x_{i-1}-a_{i-1};
\]

那么我们再一次思考一下,是不是这个东西和数学中的中位数很像啊(想一想,为什么?)

设\(c_i=\sum_{j=1}^{i-1}a_j-M\)

那么\(c_i=c_{i-1}+a_i-M\)

然后答案就是什么呢?

\(x_i=abs(x_1-c_{i-1})\)

显然就是中位数了。

然后就可以维护了。

//|--------------------------------|\\
//|author:Biscuit46 |\\
//|mail:m18890085125@163.com |\\
//|Time:2018.10.29 16:33 |\\
//|Link:www.cnblogs.com/Biscuit46 |\\
//|--------------------------------|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define ll long long
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
using namespace std;
inline int gi(){
int sum=0,f=1;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
inline ll gl(){
ll sum=0,f=1;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=1000010;
long long a[N],C[N];
int main(){
int i,j,n,m,k;
while(scanf("%d",&n)==1){
ll tot=0;
for(i=0;i<n;i++)
a[i]=gl(),tot+=a[i];
ll M=tot/n;C[0]=0;
for(i=1;i<n;i++)
C[i]=C[i-1]+a[i]-M;
sort(C,C+n);
long long x=C[n/2],ans=0;
for(i=0;i<n;i++)
ans+=abs(x-C[i]);
printf("%lld\n",ans);
}
return 0;
}

【题解】 UVa11300 Spreading the Wealth的更多相关文章

  1. Uva11300 Spreading the Wealth

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

  2. UVA11300 Spreading the Wealth 题解

    题目 A Communist regime is trying to redistribute wealth in a village. They have have decided to sit e ...

  3. (洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币

    bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p251 ...

  4. UVA11300 Spreading the Wealth 数学

    前方数学警告 题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&am ...

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

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

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

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

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

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

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

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

  9. Math - Uva 11300 Spreading the Wealth

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

随机推荐

  1. Java中的阻塞和非阻塞IO包各自的优劣思考(经典)

    Java中的阻塞和非阻塞IO包各自的优劣思考 NIO 设计背后的基石:反应器模式,用于事件多路分离和分派的体系结构模式. 反应器(Reactor):用于事件多路分离和分派的体系结构模式 通常的,对一个 ...

  2. tomcat7.0在centos7下中文乱码问题解决汇总

    1. 系统级的中文设置 Centos7跟之前的6和5安装的中文字符命令都不一样 [root@iZ25bdzgev8Z ~]# locale #查看当前系统的语言环境 [root@iZ25bdzgev8 ...

  3. Py修行路 python基础 (十三)匿名函数 与 内置函数

    一.匿名函数  1.定义: 匿名函数顾名思义就是指:是指一类无需定义标识符(函数名)的函数或子程序. 2.语法格式:lambda 参数:表达式 lambda语句中,开头先写关键字lambda,冒号前是 ...

  4. (转)CSS布局-负边距-margin

    css中的负边距(negative margin)是布局中的一个常用技巧,只要运用得合理常常会有意想不到的效果.很多特殊的css布局方法都依赖于负边距,所以掌握它的用法对于前端的同学来说,那是必须的. ...

  5. scala 在vim中的语法高亮

    https://github.com/derekwyatt/vim-scala 提供了一个选择 看install手册,发现两个命令都是必须的. mkdir -p ~/.vim/{ftdetect,in ...

  6. 解决mybatis中转义字符的问题

    xml格式中不允许出现类似“>”这样的字符,有如下两种解决方法 方法一:使用转义字符 SELECT * FROM test WHERE 1 = 1 AND start_date <= CU ...

  7. oracle数据库导入导出数据

    导出命令 exp username/password@192.168.x.xx/orcl file='D:\20170126.dmp' log='D:\20170126.log' 导入命令 imp u ...

  8. 游戏引擎架构Note2

    [游戏引擎架构Note2] 1.视觉属性(visual property)决定光线如何与物体表面产生交互作用. 2.一个Mesh所使用三角形的多少可以用细致程度(level-of-detail,LOD ...

  9. 【总结整理】KANO 模型

    c 基本(必备)型需求——Must-beQuality/ Basic Quality. 期望(意愿)型需求——One-dimensional Quality/ Performance Quality. ...

  10. SpringBoot:阿里数据源配置、JPA显示sql语句、格式化JPA查询的sql语句

    1 数据源和JPA配置 1.1 显示sql配置和格式化sql配置 者两个配置都是属于hibernate的配置,但是springdatajpa给我们简化了:所有hibernate的配置都在jpa下面的p ...