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. hdu1106 排序(字符串分割,strtok+sscanf)

    排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  2. centos7下搭建django

    安装环境:centos7.4 1 安装nginx yum install nginx 注:尝试过在本地和腾讯云上安装,使用同一条命令:在本地安装提示没有可用安装包,云上安装正常 启动nginx,并启用 ...

  3. 打印队列 (Printer Queue,ACM/ICPC NWERC 2006,UVA12100)

    题目描述: 题目思路: 使用一个队列记录数字,一个优先队列记录优先级,如果相等即可打印: #include <iostream> #include <queue> using ...

  4. 爬虫1.1-基础知识+requests库

    目录 爬虫-基础知识+requests库 1. 状态返回码 2. URL各个字段解释 2. requests库 3. requests库爬虫的基本流程 爬虫-基础知识+requests库 关于html ...

  5. joomla 出现The file Cache Storage is not supported on this platform;

    错误提示:The file Cache Storage is not supported on this platform:在这个平台上不支持文件缓存存储 出现这样的原因很简单,有两个文件夹不可写,这 ...

  6. 《剑指offer》---顺时针打印矩阵

    本文算法使用python3实现 1. 问题1 1.1 题目描述:   输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 ...

  7. Debian 7 amd64 + fbterm + ucimf

    前段时间,显示器出了问题,导致Debian下只有终端显示正常,桌面显示效果很是摇晃模糊.遂起了念头,能不能在终端下就能完成日常的工作. google了很久,终于知道fbterm可以在终端下显示中文,加 ...

  8. vue-cli2使用cdn方式引入cytoscape

    1. index.html头部引用 <script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.2.19/cytos ...

  9. 解决编写 xml 没有代码提示

    有时候在编写 struts.xml 会没有代码提示,一般是因为没有联网导致的,或者之前配置过 dtd 文件 url,但是文件路径之后被修改了. 解决方案有: 让电脑联网 修改 dtd 的本地路径以及 ...

  10. table中的td限制宽度width也不能让字符过长变成省略号生效?

    table中的td限制宽度width也不能让字符过长变成省略号生效? http://blog.csdn.net/java_mr_zheng/article/details/49423247 CSS t ...