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 ---------------------------------------------------------------- ...
随机推荐
- 剑指offer--面试题14
#include "stdafx.h" #include <iostream> using namespace std; //调整数组顺序使奇数位于偶数前 void O ...
- linux挂载windwos共享文件
mount -t cifs -o useaname=***,password=*** //172.16.82.32/test /mnt username,password分别为windows那边允许共 ...
- override equals in Java
equals() (javadoc) must define an equality relation (it must be reflexive, symmetric, and transitive ...
- Javascript实现两张图片的延迟加载
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- HDU 1452 Happy 2004(因子和的积性函数)
题目链接 题意 : 给你一个X,让你求出2004的X次方的所有因子之和,然后对29取余. 思路 : 原来这就是积性函数,点这里这里这里,这里讲得很详细. 在非数论的领域,积性函数指所有对于任何a,b都 ...
- 简单易懂的现代魔法——Play Framework攻略4
接前文:简单易懂的现代魔法——Play Framework攻略3 1.The Object 时隔2个多月,Play Framework系列又更新了,本次的主题是:利用Play Framework实现R ...
- 修改linux文件/文件夹权限
事情缘起:在VirtualBox虚拟机Ubuntu 12.04里通过共享文件夹从物理机拷贝jdk,拷贝过来之后不能正常使用.用javac -version命令不能查看java版本信息,sudo可以.原 ...
- springMVC获取request和response
转载:http://blog.sina.com.cn/s/blog_7085382f0102v9jg.html 1.参数 例如: @RequestMapping("/test") ...
- 用static关键字修饰类
Java里面static一般用来修饰成员变量或函数.但有一种特殊用法是用static修饰内部类,普通类是不允许声明为静态的,只有内部类才可以.被static修饰的内部类可以直接作为一个普通类来使用,而 ...
- Servlet编写登录界面
package com.mhb; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.Servlet ...