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 (思维题 中位数模型)的更多相关文章

  1. 数学/思维 UVA 11300 Spreading the Wealth

    题目传送门 /* 假设x1为1号给n号的金币数(逆时针),下面类似 a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[ ...

  2. UVa 11300 Spreading the Wealth(有钱同使)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...

  3. uva 11300 - Spreading the Wealth(数论)

    题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...

  4. UVA - 11300 Spreading the Wealth(数学题)

    UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...

  5. Uva 11300 Spreading the Wealth(递推,中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  6. UVA 11300 Spreading the Wealth (数学推导 中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  7. Math - Uva 11300 Spreading the Wealth

    Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...

  8. [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]

    Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...

  9. 【思维】UVA 11300 Spreading the Wealth

    题目大意 vjudge链接 有n个人围圆桌而坐,每个人有Ai个金币,每个人可以给左右相邻的人一些金币. 若使得最终所有人金币数相等,求最小金币转移数. 数据范围 n<1000001 样例输入 3 ...

随机推荐

  1. 【二】H.264/MPEG-4 Part 10 White Paper 翻译之 Prediction of Intra Macroblocks

    翻译版权所有,转载请注明出处~ xzrch@2018.09.14 ------------------------------------------------------------------- ...

  2. poj3984迷宫问题(dfs+stack)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35426   Accepted: 20088 Descriptio ...

  3. 使用 Fiddler工具模拟post四种请求数据

    post请求主体详解: 对于get请求来说没有请求主体entity-body.对于post请求而言,不会对发送请求的数据格式进行限制,理论上你可以发任意数据,但是服务器能不能处理就是另一回事了.服务器 ...

  4. Windows10系统,安装appium之坑

    本文主要讲述如何在 Windows10 系统上通过 npm 命令行安装 appium 应该有很多小伙伴在使用cnpm安装appium时遇到过各种报错,比如这样: 相信很多的小伙伴都会遇到这样的报错,导 ...

  5. Zookeeper与Eureka的区别

    Zookeeper与Eureka的区别 想要了解Zk与eureka的区别首先要知道CAP定理 CAP定理 Mysql强一致性(数据唯一出处),设计数据库设计的三范式 (表必须有主键:表不能有重复的列: ...

  6. JavaScript 正则

    元字符 预定义类 边界 ^在中括号中时,匹配非hello的 str = 'hello world' str.match(/[^hello]/g) //[" ", "w&q ...

  7. Docker学习记录3: 搭建 Private Registry

    恩, Private Registry 特别好搭建, 只要依照官方文档, 很容易安装... https://docs.docker.com/registry/deploying/ 5000是个常用的端 ...

  8. 5.hadoop常用命令

    1. 单独启动和关闭hadoop服务 启动名称节点 #hadoop-daemon.sh start namenode 启动数据节点 #hadoop-daemons.sh start datanode ...

  9. 有道云笔记Markdown使用

    目录 使用规则 代码高亮 制作待办事项 高效绘图 基本规则 使用规则 代码高亮 #include <iostream> #include <string> using name ...

  10. ZOJ 3644 Kitty's Game(数论+DP)

    Description Kitty is a little cat. She is crazy about a game recently. There arenscenes in the game( ...