【题解】 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 ---------------------------------------------------------------- ...
随机推荐
- 理解contextmanager
同事在查看网络问题导致虚拟机状态一直pause时,在一段代码(见以下)处产生了疑惑.问我,我也是一头雾水.后同事找到参考文章(1),算是了解了个大概.而我对contextmanager的工作产生了兴趣 ...
- Composer安装使用
Composer 是 PHP5.3以上 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 1.下载 2.安装成功 3.配置地址 4.安装代码库 镜像 配置json
- php中mb_strlen,mb_substr根据中文长度截取字符串
大于8截取,小于等于则不截取. 结合thinkphp模板引擎规则,代码如下: <,,'utf-8'}..<else/>{sh:$vo.name}</if> 这里if中的函 ...
- CXF动态客户端如何优化JaxWsDynamicClientFactory.createClient -- 慢
在CXF动态创建客户端时,如下: JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); Client ...
- python利用utf-8编码判断中文字符
下面这个小工具包含了 判断unicode是否是汉字,数字,英文,或者其他字符. 全角符号转半角符号. unicode字符串归一化等工作. 还有一个能处理多音字的汉字转拼音的程序,还在整理中. #!/u ...
- ORA-00372此时无法修改文件5 ORA-01110数据文件5'M:\WWFDATA.dbf'
错误提示如下图: ORA-00372此时无法修改文件5 ORA-01110数据文件5'M:\DB_DATA\SEINESCMDB\WWFDATA_DATA01.dbf' 分析原因及解决方法: 1.查看 ...
- codeforces:Roads in the Kingdom分析和实现
题目大意:国家有n个城市,还有n条道路,每条道路连通两个不同的城市,n条道路使得所有n个城市相互连通.现在国家经费不足,要关闭一条道路.国家的不便度定义为国家中任意两个不同的城市之间的距离的最大值,那 ...
- 【bzoj1017】[JSOI2008]魔兽地图DotR
1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1658 Solved: 755[Submit][S ...
- rsa 数学推论
RSA加密算法是最常用的非对称加密算法,CFCA在证书服务中离不了它.但是有不少新来的同事对它不太了解,恰好看到一本书中作者用实例对它进行了简化 而生动的描述,使得高深的数学理论能够被容易地理解.我们 ...
- c++原型模式(Prototype)
原型模式是通过已经存在的对象的接口快速方便的创建新的对象. #include <iostream> #include <string> using namespace std; ...