UVA.11300 Spreading the Wealth (思维题 中位数模型)
UVA.11300 Spreading the Wealth (思维题)
题意分析
现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人手中的金币个数相等,并求出转移金币的最小个数。保证(Σa[i])/n为整数。
第一眼没有思绪,这种推导方式还是第一次见到。
设ai为第i个人初始金币数量,xi为第i个人转移给i-1个人金币的数量(i为1表示转移给第n个人),(Σa[i])/n = m。
有了上述的基础,可以写出每个人的金币表达式:
即每个人最终的金币数量 = 他的初始化数量-转移走的数量+转移来的数量
a1 - x1 + x2 = m
a2 - x2 + x3 = m
a3 - x3 + x4 = m
……
an - xn + x1 = m
可以写出n个式子,但是这n个式子中只有n-1个有用,因为任意一个式子都可以由剩下的n-1个式子推出。读者可以一试:将1至n-1个式子叠加,可以得到第n个式子。
于是我们要想办法利用这n-1个式子,关键就是:转换成单变量。
我们此处都转换成以x1为变量的式子,即用x1表示xi(i≠1):
x2 = m-a1+x1
x3 = m-a2+x2 = m-a2+m-a1+x1 = 2m-a2-a1+x1
x4 = m-a3+x3 = m-a3+2m-a2-a1+x1 = 3m-a3-a2-a1+x1
……
xn = (n-1)m - Σai( 1<=i<=(n-1) ) + x1
再由于n,m,ai均为常数,上述xi的表达式变量均为x1,但是这样依旧不够,仍旧需要转化成中位数模型
先抛开原题,考虑这样一个问题:
数轴上有n个点,现在求到这n个点的距离和最小的点在哪里。答案就是这些点坐标的中位数。证明笔者就不给出了,可以参考大白书,有兴趣的读者可以一试。
回到原题,我们将xi的表达式改写:
x1 = x1 - 0
x2 = x1 -(a1-m)
x3 = x1 -(a2+a1-2m)
x4 = x1 -(a3+a2+a1-3m)
……
别忘了我们xi表达的是金币转移的数量,即为正数,故所求需要加绝对值,那么 :
|x1| = |x1 - 0|
|x2| = |x1 -(a1-m)|
|x3| = |x1 -(a2+a1-2m)|
|x4| = |x1 -(a3+a2+a1-3m)|
这么一写,是不是就是上面说的中位数的模型了,其中0,(a1-m),(a2+a1-2m),(a3+a2+a1-3m)……不妨看成是数轴上一系列的点。x1即为需要求出的中位数。 求出中位数后,带入上式累加,即可求出最终的答案。
代码总览
#include <cstdio>
#include <algorithm>
#include <cmath>
#define nmax 1000005
#define ll long long
using namespace std;
ll a[nmax],c[nmax];
int main()
{
int n;
while(scanf("%d",&n) != EOF){
ll sum = 0;
for(int i = 0; i<n; ++i){scanf("%d",&a[i]);sum+=a[i];}
ll t = sum/n;c[0] = 0;
//ci 为对应的0,(a1-m),(a2+a1-2m),(a3+a2+a1-3m)……
for(int i = 1 ;i<n; ++i)
c[i] = c[i-1] + a[i] - t;
sort(c,c+n);
//pos为中位数
ll pos = c[n/2],ans = 0;
for(int i = 0; i<n;++i) ans+=fabs(pos-c[i]);
printf("%lld\n",ans);
}
return 0;
}
UVA.11300 Spreading the Wealth (思维题 中位数模型)的更多相关文章
- 数学/思维 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(有钱同使)
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个人,每个人有一定数量的金币,金币的总数能被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
题目大意 vjudge链接 有n个人围圆桌而坐,每个人有Ai个金币,每个人可以给左右相邻的人一些金币. 若使得最终所有人金币数相等,求最小金币转移数. 数据范围 n<1000001 样例输入 3 ...
随机推荐
- .net MVC 图片水印,半透明
filter:alpha(opacity=50):这个是为IE6设的,可取值在0-100,其它三个0到1.-moz-opacity:0.5; 这个是为了支持一些老版本的Mozilla浏览器.-khtm ...
- 快速写一个babel插件
es6/7/8的出现,给我们带来了很多方便,但是浏览器并不怎么支持,目前chrome应该是支持率最高的,所以为了兼容我们只能把代码转成es5,这应该算是我们最初使用babel的一个缘由,随着业务的开发 ...
- hdu1181变形课(floyd)
变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submis ...
- centos下php环境安装redis
一.安装redis(仅可在服务器使用,尚不能通过浏览器访问) (1)首先下载redis:wget http://download.redis.io/releases/redis-4.0.9.tar.g ...
- Python3开启Http服务
在CMD命令行输入D: 切换到D盘, 然后输入 python -m http.server 8000 开启HTTP服务: 在浏览器地址栏输入 http://localhost:8000/
- Python3安装pywin32模块
假如你安装的是Python3.6, 那么可以直接用PyCharm或者pip安装pywin32模块: 但是, 由于我安装的是Python3.7, 所以PyCharm或者pip都无法成功安装pywin32 ...
- 初学Direct X(8) ——碰撞检测
初学Direct X(8) --碰撞检测 真正让一个游戏鹤立鸡群的是程序对碰撞的响应有多好,这里介绍两种检测的方法: 1) 基于边框的碰撞检测 2) 基于距离的碰撞检测 1. 基于边框的碰撞检测 1. ...
- Siki_Unity_1-6_C#编程初级教程(未学)
Unity 1-6 C#编程初级教程 任务1:C#和.Net框架 C#是.Net里的一个成分 2002年微软发布第一个.Net框架(多平台,行业标准,安全性) .Net框架 IDE编程工具 --产生- ...
- Android开发-API指南-<uses-permission>
<uses-permission> 英文原文:http://developer.android.com/guide/topics/manifest/uses-permission-elem ...
- WCF服务库创建-20140919
1. 创建wcf服务库 2. 宿主到web程序上 // 宿主wcf服务库 RouteTable.Routes.Add(new ServiceRoute("ctserver.dll" ...