UVA 11300 Spreading the Wealth
题目大意:n个人手中有些金币,每个人可给相邻两个人一些金币,使得最终每个人手中金币数相同,求被转手的金币最少数 m为最终每个人手中的金币数,a1,a2,a3,...,an为每个人开始时手中的金币数,x1,x2,x3,...,xn为每个人转手的金币数
- 假设 4个人 号分别为 1 2 3 4
x2 :为2号给1号的金币数
x3 :为3号给2号的金币数
x4 :为4号给3号的金币数
x1 :为1号给4号的金币数
设:c1 = a1 - m(即:cn = an - m);
C1 = c1 = a1 - m;
C2 = c1 + c2 = C1 + c2 = C1 + a2 - m;
C3 = c1 + c2 + c3 = C2 + c3 = C2 + a3 - m;
...
Cn = C(n - 1) + an - m;
1号 :m = a1 + x2 - x1; x2 = m + x1 - a1 = x1 - c1 = x1 - C1;(1号最终所得金币为其原有的金币加上2号所给的减去其给4号的金币所得,下面同理)
2号 :m = a2 + x3 - x2; x3 = m + x2 - a2 = x2 - c2 = x1 - c1 - c2 = x1 - C2;
3号 :m = a3 + x4 - x3; x4 = m + x3 - a3 = x3 - c3 = x1 - c1 - c2 - c3 = x1 - C3;
...
xn = x1 - C(n - 1);
求转手金币的最小值(即求x1 + x2 + x3 +...+xn的最小值即求x1+|x1-C1|+|x1-C2|+|x1-C3|+...+|x1-C(n-1)|的最小值)
其几何意义为求一个点到n个点距离和的最小值(即数轴上点x1到Ci的距离和的最小值)
若想使其距离和最小,则x1需满足的条件是:点x1必须为n个点中的中位数
若点数n为奇数则点x1必须与中间那个点重合(即中位数)
若点数n为偶数则点x1必须在中间两个点之间(仍是中位数) #include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define N 1000010 long long a[N], c[N], sum, ans, m, x1;
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
} int main()
{
int n, i;
while(scanf("%d", &n) != EOF)
{
sum = ans = ;
for(i = ; i < n ; i++)
{
scanf("%lld", &a[i]);
sum += a[i];
}
m = sum / n;
c[] = ;
for(i = ; i < n ; i++)
c[i] = c[i - ] + a[i] - m;
qsort(c, n, sizeof(c[]), cmp);
x1 = c[n / ];
for(i = ; i < n ; i++)
ans += fabs(x1 - c[i]);
printf("%lld\n", ans);
}
return ;
}
UVA 11300 Spreading the Wealth的更多相关文章
- UVa 11300 Spreading the Wealth(有钱同使)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- uva 11300 - Spreading the Wealth(数论)
题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...
- UVA.11300 Spreading the Wealth (思维题 中位数模型)
UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...
- 数学/思维 UVA 11300 Spreading the Wealth
题目传送门 /* 假设x1为1号给n号的金币数(逆时针),下面类似 a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[ ...
- 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 ...
- 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 ---------------------------------------------------------------- ...
- [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]
Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...
- UVa 11300 Spreading the Wealth 分金币
圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值,比如 n = 4, ...
随机推荐
- ubuntu10.04开启root登陆
半年没有用ubuntu了,以前用的是8.10,现在装了一个10.04,第一印象就是登陆窗口变了,哎,比较喜欢用root用户登录系统,不喜欢非root用户,做任何事都要来一下sudo,10.04的登陆窗 ...
- 终极解决方案:windows10开机黑屏,死机
windows10开机黑屏,死机一般情况都是由于双显卡中的独立显卡驱动造成的! 那么试着升级一下你的BIOS吧!一定要在官网下载你对应的BIOS驱动,然后双击安装,这个时候就别再动机子了,让他自己刷新 ...
- Google发布SSLv3漏洞简要分析报告
今天上午,Google发布了一份关于SSLv3漏洞的简要分析报告.根据Google的说法,该漏洞贯穿于所有的SSLv3版本中,利用该漏洞,黑客可以通过中间人攻击等类似的方式(只要劫持到的数据加密两端均 ...
- 【C#学习笔记】播放wma/mp3文件
using System; using System.Runtime.InteropServices; namespace ConsoleApplication { class Program { [ ...
- Oracle alter index rebuild 与 ORA-08104 说明
在ITPUB 论坛上看到的一个帖子,很不错.根据论坛的帖子重做整理了一下. 原文链接如下: alter index rebuild online引发的血案 http://www.itpub.net/t ...
- .NET之美——C#中的委托和事件(续)
C#中的委托和事件(续) 引言 如果你看过了 C#中的委托和事件 一文,我想你对委托和事件已经有了一个基本的认识.但那些远不是委托和事件的全部内容,还有很多的地方没有涉及.本文将讨论委托和事件一些更为 ...
- C# chart,有关如何在鼠标移动到Series上时显示节点及数据 (有待继续更新)
一.效果与思路 效果: 解决方案1 用chart的mousemove时间,实时跟踪鼠标最近的X轴的位置,然后把cursorX设置到那个位置上,让用户知道我是选的那一个X的值,同时用tooltip显示该 ...
- Ubuntu 出现 apt-get问题的解决方法
ubuntu 10.10 sudo apt-get update 404 Not Found or W: Failed to fetch http://cn.old-releases.ubuntu. ...
- 打印Ibatis最终的SQL语句
在项目开发时都大家都希望将SQL在后台打印出来,以帮助开发以及后续的bug修改.如果用JDBC那么可以方便的打印,可使用ibatis就不知道怎么办了,最近在网上找了一段log4j的配置可以很保姆的处理 ...
- Android ViewPager欢迎页+引导页+进入首页
import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences ...