设第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. Oracle导入导出dmp文件

    目 录 目             录...3 1      说明...3 2      导出dmp文件...3 3      导入dmp文件...5 3.1            环境准备...5 ...

  2. java9-2形式参数

    1.形式参数: 基本类型(太简单) 引用类型 类名:(匿名对象的时候其实我们已经讲过了)需要的是该类的对象 抽象类:需要的是该抽象的类子类对象 接口:需要的是该接口的实现类对象 A. 类名:(匿名对象 ...

  3. Redis word bak

    @font-face { font-family: "Arial"; }@font-face { font-family: "Courier New"; }@f ...

  4. poj 1411 Calling Extraterrestrial Intelligence Again(超时)

    Calling Extraterrestrial Intelligence Again Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  5. Elasticsearch集群中处理大型日志流的几个常用概念

    之前对于CDN的日志处理模型是从logstash agent==>>redis==>>logstash index==>>elasticsearch==>&g ...

  6. Ubuntu优化-修改启动级别

    一 修改Ubuntu启动级别 sudo apt-get install sysv-rc-conf 执行: sysv-rc-conf 打x的表示开机启动. 二 启动级别 Ubuntu默认启动级别为2 r ...

  7. iBATIS.net获取运行时sql语句

    [本文原创,第一次离首页如此之近.发在候选区攒攒rp,管理员看着不合适可以撤下.] 虽然只在iBatis的世界里小打小闹匆匆数月,却历经数次与领导和同事激辩,再通过不懈努力学习和开发积累,楼猪终于被它 ...

  8. Fragment中监听onKey事件,没你想象的那么难。

    项目中越来越多的用到Fragment,在用Fragment取代TabHost的时候遇到了一个问题,我们都知道,TabHost的Tab为Activity实例,有OnKey事件,但是Fragment中没有 ...

  9. 信息安全系统设计基础实验一 20135211&20135216

    北京电子科技学院(BESTI) 实 验 报 告 封面 课程:信息安全系统设计基础              班级:1352           姓名:(按贡献大小排名)李行之 刘蔚然          ...

  10. c# 模拟get和post

    private string HttpPost(string Url, string postDataStr)         {             HttpWebRequest request ...