【题解】 UVa11300 Spreading the Wealth
题目大意
圆桌旁边坐着\(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\)
所以进一步转换就是
\]
那么我们再一次思考一下,是不是这个东西和数学中的中位数很像啊(想一想,为什么?)
设\(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的更多相关文章
- Uva11300 Spreading the Wealth
设第i个人需要给第i+1个人的金币数为xi(xi为负代表收到钱),列出一大堆方程. 设第i个人给第i-1个人的钱为xi(xi<0表示第i-1个人给第i个人钱).计算出最后每个人应该有的钱m,解方 ...
- UVA11300 Spreading the Wealth 题解
题目 A Communist regime is trying to redistribute wealth in a village. They have have decided to sit e ...
- (洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币
bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p251 ...
- UVA11300 Spreading the Wealth 数学
前方数学警告 题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&am ...
- UVa 11300 Spreading the Wealth(有钱同使)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- UVA 11300 Spreading the Wealth (数学推导 中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- uva 11300 - Spreading the Wealth(数论)
题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...
- Uva 11300 Spreading the Wealth(递推,中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- Math - Uva 11300 Spreading the Wealth
Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...
随机推荐
- (转) 读懂IL
引言 转自园子里的一片关于IL的好文,分享的同时,方便自己今后查阅. 原文链接:http://www.cnblogs.com/brookshi/p/5225801.html ------ 略过作者调侃 ...
- float型数据与字节数组的转化
float型数据与字节数组的转化 字节(float)浮点数为例. 一.C语言 转化常见的方法有: 1.强制指针类型转换. [html] view plain copy //转换float数据到字节数组 ...
- 一个电脑同时运行 64bit 和 32bit 的eclipse 如何匹配 jdk环境
一个电脑同时运行 64bit 和 32bit 的 eclipse 如何匹配 jdk环境 1 eclipse 分 64bit 和 32bit 两种. 64bit的eclipse 只能搭配 64b ...
- python web指纹获取加目录扫描加端口扫描加判断robots.txt
前言: 总结上几次的信息收集构造出来的. 0x01: 首先今行web指纹识别,然后在进行robots是否存在.后面是目录扫描 然后到使用nmap命令扫描端口.(nmap模块在windows下使用会报停 ...
- 第十二章 Java内存模型与线程(待续)
············
- linux 下安装mysql-5.7.16
1.解压tar -xvf mysql的包 tar -xvf mysql-5.7.16-1.el6.x86_64.rpm-bundle.tar(mysql 官网中即可找到) 2.查看是否需要卸载安装时候 ...
- pandas层级索引
层级索引(hierarchical indexing) 下面创建一个Series, 在输入索引Index时,输入了由两个子list组成的list,第一个子list是外层索引,第二个list是内层索引. ...
- flask系列五之flask_script
1.一个简单的例子 在工程里面新建一个Python文件命名为manage.py (1)安装flask_script包,然后引入 from flask_script import Manager fro ...
- Mycat实战之配置EP分片
ER分片介绍 以mycat逻辑库里面自带的例子,例如客户(CUSTOMER)跟订单(orders)以及订单条目(orders_item),订单条目依 赖订单表,订单表依赖客户,这样客户与订单以及订单条 ...
- 解读show slave status 命令判断MySQL复制同步状态
解读show slave status 命令判断MySQL复制同步状态 1. show slave status命令可以显示主从同步的状态 MySQL> show slave status \G ...