UVa 11300 Spreading the Wealth 分金币
圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除。每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等。你的任务是求出被转手的金币数量的最小值,比如 n = 4, 且 4 个人的金币数量分别为 1, 2, 5, 4 时,只需要转移 4 枚金币(第 3 个人给第 2 个人两枚,第 2 个人和第 4 个人分别给第 1 个人1 枚金币)即可实现每个人手中的金币数目相等。
假定 1 号给了 2 号 4 枚金币,而 2 号给了 1 号 1 枚金币,这样等同于 1 号给了 2 号 3 枚金币,而 2 号没有给 1 号金币。用 X2 表示 2 号给 1 号的金币数目,以此类推:
Ai – Xi + X i + 1 = M 其中A为原有的金币,M 最终状态每人手中的金币数
这样,可以得到 1 ~ n 一共 n 个方程。但是无法用这 n 个方程得出最终答案。不过可以用 X1 来表示其他所有的 Xi 。于是,这道题目变成了求单变量最值的问题。
A1 – X1 + X2 = M –> X2 = M – A1 + X1 = X1 – C1 其中 C1 = A1 – M
A1 – X2 + X3 = M –> X3 = 2 * M – A2 + X3 = X1 – C2 其中 C2 = A1 + A2 – 2 * M
……
题目的目的就是让所有的 Xi 的绝对值之和最小,而 | Xi – Ci | 的意义是数轴上 Xi 到 Ci 的距离。所以题目转化成了 n 个点到某点的最小距离和为多少。
自然而然的就想到了中位数,于是就找出 C 的中位数,最后一个for循环就能找出答案。
附AC代码:
1: #include <stdio.h>
2: #include <math.h>
3: #include <iostream>
4: #include <cstdarg>
5: #include <algorithm>
6: #include <string.h>
7: #include <stdlib.h>
8: #include <string>
9: #include <list>
10: #include <vector>
11: #include <map>
12: #define LL long long
13: #define M(a) memset(a, 0, sizeof(a))
14: using namespace std;
15:
16: void Clean(int count, ...)
17: {
18: va_list arg_ptr;
19: va_start (arg_ptr, count);
20: for (int i = 0; i < count; i++)
21: M(va_arg(arg_ptr, int*));
22: va_end(arg_ptr);
23: }
24:
25: LL c[1000009], a[1000009];
26:
27: int main()
28: {
29: int n;
30: while (~scanf("%d", &n))
31: {
32: LL m = 0;
33: for (int i = 0; i < n; i++)
34: {
35: scanf("%d", &a[i]);
36: m += a[i];
37: }
38: m /= n;
39: for (int i = 1; i < n; i++)
40: c[i] = c[i - 1] + a[i] - m;
41: sort(c, c + n);
42: LL x1 = c[n / 2];
43: LL res = 0;
44: for (int i = 0; i < n; i++)
45: res += abs(x1 - c[i]);
46: printf("%lld\n", res);
47: }
48: return 0;
49: }
UVa 11300 Spreading the Wealth 分金币的更多相关文章
- [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(数学题)
UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...
- 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(有钱同使)
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 (数学推导 中位数)
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 ---------------------------------------------------------------- ...
随机推荐
- git如何ignore
今天新建了一个项目传到git上,但是每次编译都会有一些无用的文件生成,于是就编写了ignore.但是发现无用.因为你的文件已经上传到服务器了,再编写ignore就无用了,ignore的适用是文件没上传 ...
- Java学习第一篇:变量,数据类型,运算符,流程控制(简介)
一.安装和配置jdk 1.jdk是什么? (1).jdk全称是Java Development Kit, Java开发工具包; (2).jdk是sun公司开发的; (3).jdk主要包括:jre(Ja ...
- gulp下livereload和webserver实现本地服务器下文件自动刷新
一.前言 node从v0.10.26升级(为了匹配autoprefixer)到v5.3.0后出现了gulp插件兼容问题,在nodejs下各种新的插件出现问题,需要重新配置.livereload实现ch ...
- SQLMap使用
http://www.freebuf.com/articles/web/29942.html http://sqlmap.org/ http://blog.csdn.net/zgyulongfei/a ...
- 看来要学 Asp.Net 了
C#大部分招聘都要这个:对个人用而言,太庞大了,所以对其的感观一直不咋,也就没想学了.
- Oracle 9 - 分析undo和snapshot too old错误
什么操作会生成undo INSERT生成的UNDO最少,只要记录新的rowid UPDATE生成的undo多一点,它要记录修改前的数据中的那部分. DELETE生成最多的undo, 因为它要记录整行被 ...
- Spring多资源文件properties的配置
Spring简化了加载资源文件的配置,可以通过<context:property-placeholder去加载,这个元素的写法如下: <context:property-placehold ...
- DP方程及意义
01背包 有N件物品和一个容量为V的背包.第i件物品的费用(即体积,下同)是w[i],价值是c[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 基本思路: 这是最基 ...
- Protege汉字不能正常显示问题
在Protege5.0中有下面的问题: 点击uses,汉字不能正常显示. 在qq群里面问到,可以通过设置label的方式,在对类,子类命名成英语的,点击annotations,在label中设置汉字名 ...
- hdu 4465 Candy
题解: 由题意得 需要运用: C(m,n)=exp(logC(m,n)) f[]=; ; i<=; i++) f[i]=f[i-]+log(i*1.0); double logC(int m,i ...