题意

n个人围成一圈,每个人都有一定数量的金币,金币总数可被n整除,现可将手中金币给左右相邻的人,最终使每人手中的金币数相等,求最少转移的金币数量。

思路

设a[i]给了a[i-1]x1个金币,从a[i+1]拿到x2个金币,则有

a1-x1+x2 = m (此时x1为给an的金币数) 另 c1 = a1 - m   则 x2 = x1 -c1

a2-x2+x3 = m ,则c2 = c1+a2-m   x3 = x1 - c2

...

|x1| + |x1-C1|+...+|x1-Cn-1|,要求这个最小,那么就是要x1为这些数的中位数

总结

学会数学分析的方法,总结规律并转换成代码

#include <iostream>
#include <cstdio>
#include <algorithm>
typedef long long LL;
const int maxn = 1e6 + 5;
LL a[maxn],c[maxn],tot,m; //数组a为每个人金币的数量 m为最后相等时的金币数
using namespace std;int main()
{int n;while(scanf("%d",&n) == 1) {tot = 0;for(int i = 1; i <= n; i++) {cin >> a[i];tot += a[i];}m = tot / n;c[0] = 0;for(int i = 1; i < n; i++) {c[i] = c[i-1] + a[i] - m;}sort(c,c+n);LL x1 = c[n/2],ans = 0;for(int i = 0; i < n; i++) {ans += abs(x1 - c[i]);}cout << ans <<endl;}return 0;
}

转载于:https://www.cnblogs.com/kikii233/p/5858230.html

UVa11300 - Spreading the Wealth相关推荐

  1. UVA11300 Spreading the Wealth

    UVA11300 Spreading the Wealth 思路  对于这道题,我们可以将其转化为一道线性代数的问题,设第iii个人的初始值为AiA_{i}Ai​,第iii个人给第i−1i-1i−1个 ...

  2. UVa11300 Spreading the Wealth(数学问题)

    题意:给出n个人,每个人有一些金币,可以给一些金币左边或者右边的人,最终使得每个人有相同的金币,问最小的转移金币是多少? 思路:可以假定给金币方向是逆时间方向,值可能是正负.M表示最终每个人有的金币, ...

  3. UVA11300 Spreading the Wealth 分金币 C++ (数学推导)

    参考算法竞赛入门经典训练指南 /* 最后每个人的金币:M = (A1+A2+...+An)/n(设当前每个人的金币为Ai) 设xi表示i给i+1传递给了xi个金币(xn表示n给1传递了xn个金币) 为 ...

  4. UVa11300 Spreading the Wealth 题解

    非常好的一道数学题. 原题链接(洛谷) 原题链接(UVa) 题目分析 (参考刘汝佳<算法竞赛入门经典 ⋅\cdot⋅ 训练指南>) 本身看起来很复杂.不要急,我们慢慢分析. 首先,每个人最 ...

  5. 11300 - Spreading the Wealth

    (解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...

  6. Spreading the Wealth( UVA - 11300)

    题目链接: Spreading the Wealth UVA - 11300 Problem A Communist regime is trying to redistribute wealth i ...

  7. UVA - 11300 Spreading the Wealth 中位数,递推

    UVA - 11300 Spreading the Wealth 题意: 有n个人,每个人都有一些钱,每个人都可以把任意的钱分给左右相邻的两个人(第一个人可以把钱分给第二个人和第 n 个人),求最少需 ...

  8. 题解 UVA - 11300 Spreading the Wealth

    题解 UVA - 11300 Spreading the Wealth 1:题意 A Communist regime is trying to redistribute wealth in a vi ...

  9. UVa 11300 Spreading the Wealth

    注意:给定数轴上的n 个点,在数轴上的所有点中,中位数离所有顶点的距离之和最小.  F. Spreading the Wealth  Problem A Communist regime is try ...

最新文章

  1. R语言ggplot2可视化在lines线图的尾端添加线图标签、并且去除图例实战
  2. 论面向组合子程序设计方法 之 oracle
  3. 【MySQL】sysbench压测服务器及结果解读
  4. docker 删除映像_如何在Docker中删除映像和容器
  5. .gitignore更新后如何生效
  6. java排序学习笔记
  7. ROS安装时rosdep init与rosdep update问题解决方法(2022.04.08亲测)
  8. 常用数据库及表相关操作语句
  9. 公司周刊-非常6+1—营销平台小组
  10. python selenium无头浏览器
  11. 本地搭建SVN局域网服务器
  12. python哪个版本好用-python用哪个版本好
  13. python随机图片api_用fastapi搭建随机图api(雁陎二次元随机图api开放试用)
  14. python lambda拉姆达表达式
  15. aix7.1重装6.1_优化AIX 6.1性能调整
  16. 18获得触发事件元素节点的方法
  17. catic备份mysql,Catic构建与部署
  18. 预备内容:---软件安装篇(1)
  19. 当我精通vue2的源码dep和watcher的关系时
  20. 30岁转行程序员晚了吗?分享30岁转行的经历

热门文章

  1. WPF Snoop 2.7 源码研究
  2. asp.net 后台事件掉用前台js
  3. 【C++】多线程与互斥锁【二】
  4. memset函数详细说明
  5. 配置.net 3.0开发环境
  6. 使用man在线手册页
  7. 内存问题分析的利器——valgrind的memcheck
  8. Windows与Linux之间互传文件的方法
  9. 协方差矩阵介绍及C++/OpenCV/Eigen的三种实现
  10. C++11中头文件thread的使用