设第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. Linux下php安装memcache扩展

    安装环境:CentOS 6.4 php扩展memcache的作用是为了支持memcached数据库缓存服务器,下面是安装方法. 1.下载 下载地址:http://pecl.php.net/packag ...

  2. Saltstack-初体验

    安装 rpm -Uvh http://mirrors.yun-idc.com/epel/6Server/x86_64/epel-release-6-8.noarch.rpm yum install s ...

  3. HDR 拍照模式的原理,实现及应用

    转自:http://blog.csdn.net/fulinwsuafcie/article/details/9792189 HDR 拍照:        (High Dynamic Range Ima ...

  4. QT QString类

    字符串有如下几个操作符 QString提供了一个二元的"+"操作符用于组合两个字符串,并提供了一个"+="操作符用于将一个字符串追加到另一个字符串的末尾,例如: ...

  5. C语言 复杂的栈(链表栈)

    //复杂的栈--链表栈 #include<stdio.h> #include<stdlib.h> #define datatype int//定义链表栈数据类型 //定义链表栈 ...

  6. Ta-lib 函数一览

    import tkinter as tk from tkinter import ttk import matplotlib.pyplot as plt import numpy as np impo ...

  7. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

  8. 优秀技能经验及对java学习展望

    你有什么技能比身边人强 我觉得我并没有什么技能能够比身边90%的人强,我认为我是一个平庸的人,和身边的人应该是互有长短,互相帮助的. 关于优秀技能的成功经验 我虽然没有一个强过身边90%的人的技能,但 ...

  9. ios —— UIViewAdditions 布局坐标类库

    方便大家计算视图的高度,宽度,上下左右坐标,简化代码操作,更加直观 下载地址:http://download.csdn.net/detail/humingtao2013/7511657

  10. 浩瀚先森(guohao1206.com)

    博客搬家啦,新博客地址:浩瀚先森 http://www.guohao1206.com