UVA11300 Spreading the Wealth 题解
题目
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village. Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, such that in the end, everyone has the same number of coins. Given the number of coins of each person, compute the minimum number of coins that must be transferred using this method so that everyone has the same number of coins.
一个村庄重新分配财富。 他们已经决定让每个人围坐在一张圆桌旁。 首先,每个人都将其所有财产转换为等值的硬币,这样硬币的总数就可被村庄中的人数整除。 最后,每个人都给右边的人一些硬币,给左边的人一些硬币,每个人都有相同数量的硬币。 给定每个人的硬币数量,计算必须使用此方法转移的最小硬币数量,以便每个人都拥有相同数量的硬币。
输入格式
There is a number of inputs. Each input begins with \(n (n < 1000001)\), the number of people in the village. n lines follow, giving the number of coins of each person in the village, in counterclockwise order around the table. The total number of coins will fit inside an unsigned 64 bit integer.
有多组输入。 每个输入均以数字\(n(n <1000001)\)开头,即人数.接下来\(n\)行,以逆时针方向给出村庄中每个人的硬币数量。 硬币总数将是一个无符号的64位整数。
输出格式
For each input, output the minimum number of coins that must be transferred on a single line.
对于每组输入,输出转移的最小硬币数。
输入样例
3
100
100
100
4
1
2
5
4
输出样例
0
4
题解
这道题感觉有点像数学题啊
因为一个人只能左右传硬币,所以只需要考虑左右
而每个人最开始的硬币数和最终的硬币数都是已知的,所以列方程即可

设\(coin_i\)为最开始每个人的硬币数量,\(pass_i\)为第\(i\)个人给逆时针方向的人(\(i-1\))的硬币数量,\(x\)为最终每个人的硬币数量.
这时候可能有的人就会疑惑了,为啥只给逆时针方向的人不给顺时针方向的人?
我们假设一下,\(1\)号给\(2\)号\(5\)个硬币,\(2\)号给\(1\)号\(3\)个硬币,一共转移了\(8\)个硬币,何必这么麻烦?直接让\(1\)号给\(2\)号\(2\)个硬币不就得了? 这样只需要转移\(2\)个硬币了.
注意,这个硬币数量可以是负数,也就是说,\(1\)号给\(6\)号\(-2\)个硬币,相当于6号给1号\(2\)个硬币
所以,相邻两人之间只会有一次硬币转移.
那我们擦去顺时针的:

然后就可以开始列式子了:
\(x=coin_1+pass_2-pass_1\)
\(x=coin_2+pass_3-pass_2\)
\(\dots\)
\(x=coin_n+pass_1-pass_n\)
移项
\(pass_2=x-coin_1+pass_1\)
\(pass_3=x-coin_2+\bm{pass_2}
\\ \ \ \ \ \ \ \ \ \ \ = x-coin_2+\bm{x_1-coin_1+pass_1}
\)
\(\dots\)
由题意,我们应该让\(\sum_{i=1}^n pass_i\)最小
为了方便描述,再设一个数组\(b_i\),使\(b_0=0\),\(b_i=coin_i+b_{i-1}-x\)
这样就可以方便的表示\(pass_i\),再表示一遍
\(pass_2=pass_1+x-coin_1
\\ \ \ \ \ \ \ \ \ \ \ = pass_1 - (coin_1-x)
\\ \ \ \ \ \ \ \ \ \ \ = pass_1 - b_1
\)
\(pass_3=\bm{pass_2}+x-coin_2
\\ \ \ \ \ \ \ \ \ \ \ = \bm{pass_1 - b_1} +x - coin_2
\\ \ \ \ \ \ \ \ \ \ \ = pass_1 - (\bm{coin_2+b_1-x})
\\ \ \ \ \ \ \ \ \ \ \ = pass_1 - \bm{b_2}
\)
\(\dots\)
所以
\(\sum_{i=1}^npass_i = pass_1+pass_2+pass_3+\dots+pass_n
\\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ = |pass_1 - b_1|+|pass_1 - b_2| + \dots + |pass_1 - b_{n-1}|
\)
注意,这个式子相当于数轴上\(b_1,b_2,b_3,b_4\cdots\)到\(pass_1\)的距离之和,那么使这个距离之和最小即可
这里我们可以就用中位数的性质了:
在数轴上所有n个点中,中位数离所有点的距离之和最小
求出最小的和输出即可
理解

对于这个数轴,中位数为2,与其他点距离的和为6
如果不选择中位数,比如选择1,那么与其他点距离的和为7
形象的理解,当\(pass_1\)向左移动一个单位长度时,虽然有两个点的距离缩小了\(d\),但是有3个点的距离增加了\(d\),显然结果变大
若移动两个单位长度,同理结果大于移动一个单位长度,所以当选择中位数时,结果最小.
如果出现偶数个点,

\([2,3]\)中任意一个点皆可做\(pass_1\),由于\(pass_1\)为整数并且为了写代码方便,取端点值即可
证明
给定一个从小到大的数列\(x_1,x_2,\dots x_n\)
设\(x\)是从\(x1\)到\(x_n\)与其绝对差之和最小的数.
当\(x\)在\(x_1\)与\(x_n\)之间,\(x\)与\(x_1\),\(x_n\)的距离和就是\(x_1\)到\(x_n\)的距离,
若不在\(x_1\)与\(x_n\)之间,\(x\)与\(x_1\),\(x_n\)的距离和还要额外加上一段,
则\(x\)位于\(x_1\)与\(x_n\)之间.由于\(x_1, x_n\)与它们之间的任意一点的距离之和(\(|x_i-x_1|+ |x_i- x_n\)|)都相等,等于\(x_n一x_1\),因此不需要考虑\(x_1, x_n\).
由第3点可得,\(x\)位于\(x_2\)与\(x_{n-1}\)之间,且\(x\)与\(x_2,x_{n-1}\)的距离和相等,不需要考虑.
依次类推,\(x\)就是该数列中间的那个数,或者是中间的那两个数之一, 而这个数就是中位数.
代码
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1000000 + 10;
long long coin[maxn], b[maxn], tot, x;
int main() {
int n;
while (~scanf("%d", &n)) {
tot = b[0] = 0;
for (int i = 1; i <= n; i++) {
scanf("%lld", &coin[i]);
tot += coin[i];
}
x = tot / n;
for (int i = 1; i < n; i++) b[i] = coin[i] + b[i - 1] - x;
sort(b, b + n);
long long mid = b[n / 2], ans = 0;
for (int i = 0; i < n; i++) ans += abs(mid - b[i]);
printf("%lld\n", ans);
}
return 0;
}
UVA11300 Spreading the Wealth 题解的更多相关文章
- Uva11300 Spreading the Wealth
设第i个人需要给第i+1个人的金币数为xi(xi为负代表收到钱),列出一大堆方程. 设第i个人给第i-1个人的钱为xi(xi<0表示第i-1个人给第i个人钱).计算出最后每个人应该有的钱m,解方 ...
- 【题解】 UVa11300 Spreading the Wealth
题目大意 圆桌旁边坐着\(n\)个人,每个人有一定数量的金币,金币的总数能被\(n\)整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数量相等.您的任务是求出被转手的金币的数量的最小值. ...
- (洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币
bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p251 ...
- UVA11300 Spreading the Wealth 数学
前方数学警告 题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&am ...
- UVa 11300 Spreading the Wealth(有钱同使)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- 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(数论)
题目链接: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 ...
- Math - Uva 11300 Spreading the Wealth
Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...
随机推荐
- Vue封装公共组件TarBar
github:https://github.com/zwnsyw/TabBar 一.实现简单思路 1.在页面底部有一个单独的TabBar组件1.1自定义TabBar组件,在APP中使用1.2让TabB ...
- OAuth + Security - 6 - 自定义授权模式
我们知道OAuth2的官方提供了四种令牌的获取,简化模式,授权码模式,密码模式,客户端模式.其中密码模式中仅仅支持我们通过用户名和密码的方式获取令牌,那么我们如何去实现一个我们自己的令牌获取的模式呢? ...
- RabbitMQ是什么
1.引入MQ 1.1什么是MQ MQ(Message Quene):翻译为 消息队列,通过典型的 生产者 和 消费者 模型,生产者不断向消息队列中生产消息,消费者不断的从队列中获取消息.因为消息的 ...
- 用python复制文件夹
用python复制文件 1. 根据文件夹的名称复制 需要复制的文件夹编号文件中,每一行表示一个编号,如下所示: > cat id.txt 1 2 3 ... > 目标文件的目录结构树如下所 ...
- java特性 JDK JRE JVM
1简单性 2可移植性性(跨平台) 3面向对象 4高性能 5分布式 6动态性 7多线程 8安全性JDK:java开发工具 . JRE:JDK:java运行环境 . JVM:JDK:java虚拟机
- pycharm安装破解方法
1.pycharm专业版官方下载链接:http://www.jetbrains.com/pycharm/download/#section=windows正常下载并安装 2.从https://gith ...
- List作为泛型参数实现可接收存储任意类型的List对象
原文链接:https://blog.csdn.net/eeeeasy/article/details/80999650?utm_source=blogxgwz2 在项目中遇到一个问题,想要封装一个通用 ...
- IE11下文档模式默认值是7, 而且无法更改
IE9以上是支持css3的,但是有的IE11的浏览器里面,文档模式默认值是7,而且是无法改变的,就会导致网页布局错乱 我的IE11的文档模式默认值是11 ,如下图 (打开页面按F12) 对于默认值是 ...
- .Net Core踩坑记:读取txt中文乱码
迁移.net framework的项目,有块读取txt中文转码的问题,普通的不能再普通的代码,想都没想直接copy过去,也没测,结果今天就被坑了.Core是3.1版本,这是原来的代码: string ...
- JMeter+Grafana+Influxdb搭建可视化性能测试监控平台(使用了docker)
[运行自定义镜像搭建监控平台] 继上一篇的帖子 ,上一篇已经展示了如何自定义docker镜像,大家操作就行 或者 用我已经自定义好了的镜像,直接pull就行 下面我简单介绍pull下来后如何使用 拉取 ...