【思维】UVA 11300 Spreading the Wealth
题目大意
有n个人围圆桌而坐,每个人有Ai个金币,每个人可以给左右相邻的人一些金币。
若使得最终所有人金币数相等,求最小金币转移数。
数据范围
n<1000001
样例输入
3
100
100
100
4
1
2
5
4
样例输出
0
4
思路
可以算出最后每个人的钱数m为总钱数除以人数n。
比如,1号给2号x枚金币,相当于2号给1号-x枚金币。
所以只要考虑n→n-1,n-1→n-2,……,1→n即可。
设xi为i给i-1的金币数量。
假设i初始有Ai枚金币,最终钱数为m,则Ai-xi+xi+1=M。
设C1=A1-m,C2=C1+A2-m……
则移项得到xi+1=x1-Ci
答案是|x1|+|x1-C1|+……+|x1-Cn-1|的最小值,
因此问题就变成了在数轴上有n个点,找出一个和他们距离和最小的点。
可以得到这个点就是这些数的中位数,排序即可,或者用nth_element。
【思维】UVA 11300 Spreading the Wealth的更多相关文章
- 数学/思维 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个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...
- 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个人,每个人有一定数量的金币,金币的总数能被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 ...
随机推荐
- python语法,基础
1.用缩进表达代码逻辑:推荐缩进4个空格 2.注释和续行与shell一样 在pycharm中,按ctrl + / 可以快速添加/取消注释 3.多个语句书写在同一行,可以使用分号进行分割.但不推荐 输出 ...
- Spring系列之初识Spring Spring概述
初始Spring 啥是Spring? 下面这个就是Spring Spring当然不是上面那个Spring,Spring之所以命名为Spring是因为这个开源的轻量级的开源框架的出现给软件行业带来了春天 ...
- Linux实战(18):Centos强制踢掉某登录用户的方法
[root@localhost ~]# pkill -kill -t pts/2
- PooledByteBuf内存池-------这个我现在不太懂
转载自:http://blog.csdn.net/youaremoon/article/details/47910971 http://blog.csdn.net/youar ...
- 【Java并发编程】synchronized相关面试题总结
目录 说说自己对于synchronized关键字的了解 synchronized关键字的三种使用 synchronized关键字的底层原理 JDK1.6之后对synchronized关键字进行的优化 ...
- Docker:一、开始部署第一个Asp.net应用
工具: docker desktop :一个使用Docker的IDE工具,可以理解为SourceTree,也是使用git的一个桌面化工具: kitematic :配合desctop,用来管理本地的镜像 ...
- 关于windows服务器wsus客户端配置的修改
本文环境如下: 服务器:Windows Server 2012 R2 Standard 由于公司服务器是加了域环境的,又需要将wsus客户端指向另一台wsus服务器,修改本地组策略.可能会被域控给修改 ...
- 栈帧的内部结构--局部变量表(Local Variables)
每个栈帧中包含: 局部变量表(Local Variables) 操作数栈(Opreand Stack) 或表达式栈 动态链接 (Dynamic Linking) (或指向运行时常量的方法引用) 动态返 ...
- 并发编程(八)Lock锁
一.引言 线程并发的过程中,肯定会设计到一个变量共享的概念,那么我们在多线程运行过程中,怎么保证每个先拿获取的变量信息都是最新且有序的呢?这一篇我们来专门学习一下Lock锁. 我们先来了解几个概念: ...
- Hive 窗口函数sum() over()求当前行和前面n条数据的和
前几天遇到一个这样的需求:销售总占比加起来超过75%的top分类.具体需求是这样的:商品一级分类标签下面有许多商品标签,例如运动户外一级标签,下面可能存在361°,CBA,Nike,Adidas... ...