题目大意

圆桌旁边坐着\(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. composer 发布自己的开源软件

    首先创建一个github项目. 在项目中,创建一个composer.json文件. { "name": "jiqing9006/valid", "de ...

  2. PHP封装验证类

    <?php /** * Created by PhpStorm. * User: jiqing * Date: 18-7-24 * Time: 下午4:36 * 常用验证 */ class Va ...

  3. JetBrains ReSharper Ultimate 2017.2.2激活方法

    先提供两个链接, 需要直接拿去用即可 第一个: http://xidea.online 第二个:  http://idea.iteblog.com/key.php  (我用的这个) 方法下图: 继续: ...

  4. 第10章 深入理解Session与Cookie

    需要很多Cookie时,考虑HTTP对Cookie数量和大小的限制. 几百或更多台服务器的时候,如何解决Session在多态服务器之间共享的问题. 还有一些安全问题,如Cookie被盗,Cookie伪 ...

  5. 大杀器TheFatRat

    项目地址:https://github.com/Screetsec/TheFatRat 安装TheFatRat root@sch01ar:/sch01ar# git clone https://git ...

  6. apache 不解析 php

    apache 不解析php 1.找到: AddType application/x-gzip .gz .tgz在其下面添加: AddType application/x-httpd-php .php ...

  7. mysql 存储过程变量的定义

    Mysql变量: 1.DECLARE variable_name datatype(size) DEFAULT default_value; 此处声明的相当于一个局部变量 ,在end 之后便失效. 声 ...

  8. Monthly Expense(二分--最小化最大值)

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...

  9. python实现文件下载的方法总结

    前端时间遇到一个通过url下载文件的需求,只需要简单的编写一个py脚本即可.从网上搜了下python实现文件下载的方法,总结如下,备查. 以下方法均已测试,环境win8.1  python2.6/2. ...

  10. sql2012新的系统函数&分析函数

    一 .系统函数 1.字符串类函数:不用判断类型和NULL的字符串连接CONCAT函数 SQL Server本来对字符串的连接很简单,直接使用“+”号,但是需要注意两个问题,一是必须类型都是字符串类型, ...