题目大意

vjudge链接

有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的更多相关文章

  1. 数学/思维 UVA 11300 Spreading the Wealth

    题目传送门 /* 假设x1为1号给n号的金币数(逆时针),下面类似 a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[ ...

  2. UVA.11300 Spreading the Wealth (思维题 中位数模型)

    UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...

  3. UVa 11300 Spreading the Wealth(有钱同使)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...

  4. uva 11300 - Spreading the Wealth(数论)

    题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...

  5. UVA - 11300 Spreading the Wealth(数学题)

    UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...

  6. Uva 11300 Spreading the Wealth(递推,中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  7. UVA 11300 Spreading the Wealth (数学推导 中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  8. Math - Uva 11300 Spreading the Wealth

    Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...

  9. [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]

    Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...

随机推荐

  1. Kubernetes-12:Secret介绍及演示

    Secret介绍 Secret存在的意义 Secret解决了密码.token.密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者Pod Spec中,可以以Volume或者环境变量的方式使用 ...

  2. springboot2.x基础教程:动手制作一个starter包

    上一篇博客介绍了springboot自动装配的原理.springboot本身有丰富的spring-boot-starter-xx集成组件,这一篇趁热打铁加深理解,我们利用springboot自动装配的 ...

  3. Intersection of Two Prisms(AOJ 1313)

    原题如下: Suppose that P1 is an infinite-height prism whose axis is parallel to the z-axis, and P2 is al ...

  4. css的引用关系

    总结:离div标签越近,越先被引用 先在同级目录下新建一个stylesheet(是以.css结尾的)注意:link引入进来的css中,class标签也是c1,因为html中div class=c1,因 ...

  5. ef6 code first,对已有数据库如何执行迁移

    先执行:Enable-Migrations,会生成Migrations->Configuration.cs 再执行:Add-Migrations InitialCreate – IgnoreCh ...

  6. 老男孩教育python全栈第22期Day15笔记

    day15 今日内容大纲 昨日内容回顾作业讲解 装饰器:完美的呈现了开放封闭原则.装饰器的本质:闭包. def wraper(f): def inner(*args, **kwargs): " ...

  7. vue大型项目高性能优化----想说爱你真的不容易

    一.背景   目前公司的电子合同采用表单设计器+合同业务配合实现,做了半年多后终于上线,但是下边员工普遍反映卡顿,甚至卡死,爆栈.尤其是新增和修改合同页面,因为这部分数据量大,逻辑复杂,很容易崩溃,所 ...

  8. spring boot 源码之SpringApplication

    run方法 run方法主要创建和初始化ConfigurableApplicationContext,在各个节点调用SpringApplicationRunListener的回调函数,在发送异常时调用用 ...

  9. if-else​ 条件语句

    1.  条件语句模型 Go里的流程控制方法还是挺丰富,整理了下有如下这么多种: if - else 条件语句 switch - case 选择语句 for - range 循环语句 goto 无条件跳 ...

  10. Session、Cookie、Token 【浅谈三者之间的那点事】

    Cookie 和 Session HTTP 协议是一种无状态协议,即每次服务端接收到客户端的请求时,都是一个全新的请求,服务器并不知道客户端的历史请求记录:Session 和 Cookie 的主要目的 ...