Uva 11300 Spreading the Wealth(贪心)
题目链接:https://vjudge.net/problem/UVA-11300
这道题的思路太神了,但很难想到是贪心。
用M表示每个人最终拥有的金币数。
首先假设有四个人。假设1号给2号3枚,2号又给1号5枚,那么实际上1号并没有给2号,而2号给了1号2枚。这样设$x_2$表示2号给了1号$x_2$枚。若$x_2<0$,那么就表示1号给了2号$-x_2$枚。这样我们就相当于在1号和2号之间连了一条边,表示1号2号之间硬币关系(注意是环形,所以$x_1$表示1号和4号之间的硬币关系)。
假设1号原来有$A_1$枚硬币,那么根据1号给了4号$x_1$枚,收到了$x_2$枚硬币,所以现在1号手中有$A_1+x_2-x_1$枚。根据开始的M,我们可以得到:$M=A_1+x_2-x_1$。同理,我们可以得到其他的方程。
得到n-1个方程之后,可以尝试用$x_1$表示其他的$x_i$:
对于第1个人:$M=A_1+x_2-x_1$ --> $x_2=M-A_1+x_1=x_1-C_1$(规定$C_1=A_1-M_1$);
对于第2个人:$M=A_2+x_3-x_2$ --> $x_3=M-A_2+x_2=M-A_2+(M-A_1+x_1)=2\times M-A_1-A_2+x_1=x_1-C_2$
.....
然而对于第n个人,这个式子是多余的——关于n的两个x,已在n-1和1中计算了。
现在我们希望所有$x_i$的绝对值要尽可能地小,即$|x_1|+|x_1-C_1|+\cdots +|x_1-C_{n-1}|$的最小,而它的几何意义便是在数轴上找一个点使得这个点到所有C的距离最短。我们会发现这个点便是这些数的中位数(奇偶都是)。
注意一些边界问题..
(详细证明见 蓝书P5)
AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm> using namespace std; const int maxn=; int n;
long long mid,ans,sum,M;
long long A[maxn],C[maxn]; inline void init(){
memset(A,,sizeof(A));
sum=ans=;
} int main(){
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++){
scanf("%lld",&A[i]);
sum+=A[i];
}
M=sum/n;
for(int i=;i<n;i++) C[i]=C[i-]+A[i]-M;
sort(C,C+n);
mid=C[n/];
for(int i=;i<n;i++) ans+=abs(C[i]-mid);
printf("%lld\n",ans);
}
return ;
}
AC代码
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 ...
随机推荐
- Jquery插件 之 zTree树加载
原文链接:https://blog.csdn.net/jiaqu2177/article/details/80626730 zTree树加载 zTree 是一个依靠 jQuery 实现的多功能 “树插 ...
- 今天我解决的sql中文乱码问题
昨天我终于把我的网站做好了,在电脑上准备就绪,经过测试一切正常,放上服务器上准备炫耀一下的时候,发现插进数据库的中文字段全都变成???了,检测了下,前台是utf-8,后台是utf-8,在插进数据库前我 ...
- 第三十七篇 入门机器学习——Numpy基础
No.1. 查看numpy版本 No.2. 为了方便使用numpy,在导入时顺便起个别名 No.3. numpy.array的基本操作:创建.查询.修改 No.4. 用dtype查看当前元素的数据类型 ...
- HTML的列表标签和表格标签
网页的列表和表格 列表的分类 无序列表 有序列表 自定义列表 有序列表 <!--有序列表--><ol> <li>辽宁</li> <li ...
- AcWing 148. 合并果子
#include <iostream> #include <algorithm> #include <queue> using namespace std; int ...
- AcWing 842. 排列数字
深搜的一道水题 https://www.acwing.com/problem/content/844/ 正确输入输出 #include<bits/stdc++.h> using nam ...
- 2.8 (显示、隐式、线程休眠) selenium 等待方式 ❀
http://blog.csdn.net/pf20050904/article/details/20052485 http://www.cnblogs.com/hellokitty1/p/629584 ...
- 算法_hdoj_1003
question: Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 微服务监控平台获取网关(zuul)配置列表
步骤: (1)读取zuul的配置文件,获取路由配置项信息: private static Properties props; static { String fileName = "appl ...
- night of 2019.8.14
饭后一个小时,坐下来看了一会书,是原来的同事推荐的. 最近一段时间,心一点一点平静.能平下心来去做一些事.偶尔也常常对着窗户发呆,想一些人生感悟.想到一些感悟时,常常感到没有人可以说说心里话. 丰子恺 ...