设第i个人需要给第i+1个人的金币数为xi(xi为负代表收到钱),列出一大堆方程。

设第i个人给第i-1个人的钱为xi(xi<0表示第i-1个人给第i个人钱)。计算出最后每个人应该有的钱m,解方程得xi=x1-(a1+a2+ … +a(i-1)-(i-1) * m)=x1-ti。 
答案就是让|x1-t1|+|x1-t2|+…+|x1-tn|最小,所以x1应该取这些t中的中位数。      by sdfzyhx

 /* UVa11300 - Spreading the Wealth */
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
long long x,n;
long long a[],c[];
int main(){
int i,j;
while(scanf("%d",&n)==){
long long sum=;
long long ans=;
for(i=;i<=n;i++){
scanf("%lld",&a[i]);
sum+=a[i];
}
sum/=n;
c[]=;
for(i=;i<n;i++){
c[i]=c[i-]+a[i]-sum;//C
}
sort(c,c+n);
x=c[n/];
for(i=;i<n;i++)ans+=abs(x-c[i]);
printf("%lld\n",ans);
}
return ;
}

Uva11300 Spreading the Wealth的更多相关文章

  1. UVA11300 Spreading the Wealth 数学

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

  2. UVA11300 Spreading the Wealth 题解

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

  3. 【题解】 UVa11300 Spreading the Wealth

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

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

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

  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. QTP基础学习(一)安装目录介绍

    上一篇介绍了QTP 10 安装,安装完成后就可以看到文件的目录了,这里主要介绍以下几个目录及作用. 简单介绍部分目录 1.addins:插件包 2.bin目录:可执行程序,这里存储了很多配置文件.运行 ...

  2. android strings.xml 报 is not translated in af,

    57 down vote In your ADT go to window->Preferences->Android->Lint Error Checking Find there ...

  3. Java虚拟机工作原理详解 (一)

    一.类加载器 首先来看一下java程序的执行过程. 从这个框图很容易大体上了解java程序工作原理.首先,你写好java代码,保存到硬盘当中.然后你在命令行中输入 javac YourClassNam ...

  4. mysql数据库的备份和导入

    mysqldump -u root -p --default-character-set = gbk -d demo_db>c:/appserv/www/demosql/sql1.sql//将数 ...

  5. JCS 的基本使用

    JCS 是一款简单的内存缓存,基本使用如下 1.在 classpath 下配置 cache.ccf ,可以使用默认配置 jcs.default= 2.代码 package org.zln.jcs; i ...

  6. zabbix一件漂亮的外衣配置

    http://www.cnblogs.com/yyhh/archive/2015/09/08/4792830.html

  7. Oracle11G 卸载步骤

    (之前因为不知道偶电脑是因为安装了oracle后,才导致的长达两周的开机速度要足足10分钟,以前只有一分钟不到!可以想象oracle的服务启动的强大,知道后,偶果断立即卸载掉!) 一.在oracle1 ...

  8. “插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题

    “插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题 最近做一个前端的项目,项目需要引用VLC浏览器插件,javascript在IE.Firefox等浏览器 ...

  9. python爬虫中文网页cmd打印出错问题解决

    问题描述 用python写爬虫,很多时候我们会先在cmd下先进行尝试. 运行爬虫之后,肯定的,我们想看看爬取的结果. 于是,我们print... 运气好的话,一切顺利.但这样的次数不多,更多地,我们会 ...

  10. SpringMVC数据验证

    SpringMVC数据验证——第七章 注解式控制器的数据验证.类型转换及格式化——跟着开涛学SpringMVC 资源来自:http://jinnianshilongnian.iteye.com/blo ...