【题解】 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 ---------------------------------------------------------------- ...
随机推荐
- Models-详细操作
# 单表简单查询13种方法 1.all(): 查询所有结果 all: models.表名.objects.all() book_all=models.Book.objects.all() # 结果是q ...
- maven学习4 使用Maven构建Spring项目
1. 新建一个Web项目 参考之前的博客 2.修改 pom.xml,添加Spring依赖 <project xmlns="http://maven.apache.org/POM/4.0 ...
- 详解调试Apache的mod_rewrite模块
大家都知道Apache里面的Rewrite规则是一件很蛋疼的事情,有时候只是想做一个伪静态而已,不想去研究那些复杂的规则,可官方给的规则又常常出错,出了问题我们就要调试一下,看看提交的参数被映射到了哪 ...
- flask系列一之环境搭建包安装
一,python的安装 (1)python的安装 (2)虚拟环境的配置 参考:http://www.cnblogs.com/bfwbfw/p/7995245.html 1,虚拟环境的建立 (1)使用p ...
- Android 4 学习(15):持久化:Files, Saving State and Preferences
参考<Professional Android 4 Development> 持久化:Files, Saving State and Preferences Android中的数据持久化 ...
- leetcode696
本题先寻找字符串中0变1,或者1变0的位置作为分隔位置.然后从这个分隔位置同时向左.右两侧搜索. 找到的左连续串和右连续串,都进行累计. public class Solution { public ...
- leetcode318
public class Solution { public int MaxProduct(string[] words) { ) { ; } int len = words.Length; int[ ...
- leetcode216
public class Solution { public IList<IList<int>> CombinationSum3(int k, int n) { , , , , ...
- Python函数之返回值、作用域和局部变量
一.函数返回值 说到返回值,相信大家肯定都认识,没错,就是return. 所谓返回值可以这样理解:函数外部的代码要想获取函数的执行结果,就可以在函数里用return语句把结果返回. 那具体怎么用呢?接 ...
- 788. Rotated Digits 旋转数字
[抄题]: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...