UVA 11300 Spreading the Wealth
题目大意:n个人手中有些金币,每个人可给相邻两个人一些金币,使得最终每个人手中金币数相同,求被转手的金币最少数 m为最终每个人手中的金币数,a1,a2,a3,...,an为每个人开始时手中的金币数,x1,x2,x3,...,xn为每个人转手的金币数
- 假设 4个人 号分别为 1 2 3 4
x2 :为2号给1号的金币数
x3 :为3号给2号的金币数
x4 :为4号给3号的金币数
x1 :为1号给4号的金币数
设:c1 = a1 - m(即:cn = an - m);
C1 = c1 = a1 - m;
C2 = c1 + c2 = C1 + c2 = C1 + a2 - m;
C3 = c1 + c2 + c3 = C2 + c3 = C2 + a3 - m;
...
Cn = C(n - 1) + an - m;
1号 :m = a1 + x2 - x1; x2 = m + x1 - a1 = x1 - c1 = x1 - C1;(1号最终所得金币为其原有的金币加上2号所给的减去其给4号的金币所得,下面同理)
2号 :m = a2 + x3 - x2; x3 = m + x2 - a2 = x2 - c2 = x1 - c1 - c2 = x1 - C2;
3号 :m = a3 + x4 - x3; x4 = m + x3 - a3 = x3 - c3 = x1 - c1 - c2 - c3 = x1 - C3;
...
xn = x1 - C(n - 1);
求转手金币的最小值(即求x1 + x2 + x3 +...+xn的最小值即求x1+|x1-C1|+|x1-C2|+|x1-C3|+...+|x1-C(n-1)|的最小值)
其几何意义为求一个点到n个点距离和的最小值(即数轴上点x1到Ci的距离和的最小值)
若想使其距离和最小,则x1需满足的条件是:点x1必须为n个点中的中位数
若点数n为奇数则点x1必须与中间那个点重合(即中位数)
若点数n为偶数则点x1必须在中间两个点之间(仍是中位数) #include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define N 1000010 long long a[N], c[N], sum, ans, m, x1;
int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
} int main()
{
int n, i;
while(scanf("%d", &n) != EOF)
{
sum = ans = ;
for(i = ; i < n ; i++)
{
scanf("%lld", &a[i]);
sum += a[i];
}
m = sum / n;
c[] = ;
for(i = ; i < n ; i++)
c[i] = c[i - ] + a[i] - m;
qsort(c, n, sizeof(c[]), cmp);
x1 = c[n / ];
for(i = ; i < n ; i++)
ans += fabs(x1 - c[i]);
printf("%lld\n", ans);
}
return ;
}
UVA 11300 Spreading the Wealth的更多相关文章
- UVa 11300 Spreading the Wealth(有钱同使)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- 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(数学题)
UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...
- 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 ---------------------------------------------------------------- ...
- [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 分金币
圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值,比如 n = 4, ...
随机推荐
- 运行Android应用时提示ADB是否存在于指定路径问题
打开eclipse,选择指定的Android应用工程并Run,提示: [2014-06-28 11:32:26 - LinearLayout] The connectionto adb is down ...
- BZOJ 1297 迷路(矩阵)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1297 题意:给出一个带权有向图,权值为1-9,顶点个数最多为10.从1出发恰好在T时刻到 ...
- 1287. Mars Canals(DP)
1287 水DP #include <iostream> #include<cstdio> #include<cstring> #include<algori ...
- SetCapture、ReleaseCapture、GetCapture
正常情况下,鼠标指针位于哪个窗口区域内,鼠标消息就自动发给哪个窗口.如果调用了SetCapture,之后无论鼠标的位置在哪,鼠标消息都发给指定的这个窗口,直到调用ReleaseCapture或者调用S ...
- ie下jquery ajax 80020101错误的解决方法
<script language="javascript"> <!-- function checkAll(name,isCheck){ ...
- Hibernate映射集合属性
Hibernate要求持久化集合属性字段必须声明为接口,实际的接口可以是java.util.Set,java.util.Collection,java.util.List,java.util.Map, ...
- hdu2457:DNA repair
AC自动机+dp.问改变多少个字符能让目标串不含病毒串.即走过多少步不经过病毒串终点.又是同样的问题. #include<cstdio> #include<cstring> # ...
- DrawDib函数组的使用
Microsoft的针对与设备无关位图(DIB位图),在其WIN32 SDK的Multimedia中提供了一组绘制DIB位图的高性能函数组──DrawDib函数组.DrawDib函数组是一组不依赖于图 ...
- 《C++ Primer 4th》读书笔记 第3章-标准库类型
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3911534.html
- SpringMVC——hello SpringMVC
概述: Spring的web框架围绕DispatcherServlet设计. DispatcherServlet的作用是将请求分发到不同的处理器. 与其它web MVC框架一样,Spring的web ...