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 ---------------------------------------------------------------- ...
随机推荐
- PAT-乙级-1046. 划拳(15)
1046. 划拳(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 划拳是古老中国酒文化的一个有趣的组成部分 ...
- poj 2362
回溯加剪枝 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> # ...
- mac下SVN上传.a静态库文件
在mac下很多svn管理工具默认都不能上传.a文件,但是用命令行可以解决此问题. 打开终端,cd 进入到需要上传的.a文件所在的文件夹. 确保 ls能看到.a文件 然后使用命令,如:svn add l ...
- A const field of a reference type other than string can only be initialized with null Error [duplicate]
I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveI ...
- ios iap 购买总是提示继续的解决方案
原地址:http://blog.csdn.net/kafeidev/article/details/8619984 ========================================== ...
- floodlight StaticFlowPusher 基于网段写flow,通配
flow1 = { "switch":"00:00:00:00:00:00:00:03", "name":"flow-mod-1& ...
- 关于inline-block在IE8下无效的解决方法
<style> .divClass{ width:500px; border:1px solid #ccc; } /*IE8下可以不用加(*zoom:1;*display:inline;需 ...
- Python str字符串常用到的函数
# -*- coding: utf-8 -*- x='pythonnnnnnoooo' print type(x) # <type 'str'> 输出类型 print x.capitali ...
- 【mysql的设计与优化专题(6)】mysql索引攻略
所谓索引就是为特定的mysql字段进行一些特定的算法排序,比如二叉树的算法和哈希算法,哈希算法是通过建立特征值,然后根据特征值来快速查找,而用的最多,并且是mysql默认的就是二叉树算法 BTREE, ...
- NPOI技术,
using(FileStream stream=new FileStream("C:\Users\XXXXXX\Desktop\1.xls",FileMode.Open)) ...