题目链接:uva 11300 - Spreading the Wealth

题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的金币数量相等,问说最少移动的金币数额。

解题思路:假设xi为第i个人给左手边人的金币数量,那么就有a[i] - x[i]+ x[i + 1] = aver.那么

a[1] - x[1] + x[2] = aver -> x2 = aver - a[1] + x[1]  -> x[2]= x[1] - c[1]  (c[i]为∑a[j] - aver)

a[2] - x[2] + x[3] = aver -> x3 = aver - a[2] + x[2] = aver - a[2] + aver - a[1] + x[1] = x[1] - c[2]

.....

所以就有a[n] = x[1] - c[n],然后∑|x[i]| = ∑ | x[1] - c[i] |, 然后就可以转化成在数轴选取点x[1] 到点0 , c[1] ....c[n - 1],使得距离最小,(x[1] - c[n])即为x[1]到0的距离。然后就是最取中位数计算距离。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm> using namespace std;
#define ll long long const int N = 1000005; ll n, aver, a[N], c[N]; void input() {
ll sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
sum += a[i];
} aver = sum / n;
c[0] = 0;
for (int i = 1; i < n; i++)
c[i] = c[i - 1] - a[i] + aver;
sort(c, c + n);
} ll solve() {
ll tmp = c[n / 2], ans = 0; for (int i = 0; i < n; i++)
ans += abs(c[i] - tmp);
return ans;
} int main () { while (scanf("%lld", &n) == 1) {
input();
printf("%lld\n", solve());
}
return 0;
}

uva 11300 - Spreading the Wealth(数论)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. Math - Uva 11300 Spreading the Wealth

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

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

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

  9. UVa 11300 Spreading the Wealth 分金币

    圆桌旁坐着 n 个人,每个人都有一定数量的金币,金币总数能够被 n 整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值,比如 n = 4, ...

随机推荐

  1. ios开发中加载的image无法显示

    昨天遇到一个较奇葩的问题,imageName加载的图片显示不出来,网上查了好多资料还是没找到解决的方法: 之前图片是放在项目中SupportingFiles文件下的,怎么加载都能显示图片,于是将图片拿 ...

  2. 【PHP】 foreach循环中变量引用的一道面试题

    $a = array('a','b','c'); foreach($a as &$v){} foreach($a as $v){ } var_dump($a); 现在.不要打开浏览器,猜测一下 ...

  3. Mac OS X 好用的软件包管理工具 Homebrew

    github地址:https://github.com/Homebrew/homebrew 安装方法:http://brew.sh/

  4. 不同的路径 II

    class Solution { public: /** * @param obstacleGrid: A list of lists of integers * @return: An intege ...

  5. php读取excel,以及php打包文件夹为zip文件

    1.把文件下载到本地,放在在Apache环境下2.d.xlsx是某游戏的服务器名和玩家列表,本程序只适合此种xlsx文件结构,其他结构请修改index.php源码3.访问zip.php的功能是把生成的 ...

  6. iOS: 学习笔记, Swift与Objective-C混用总结

    Swift与Objective-C交互总结 在Swift中使用Objective-C(简单) 在创建OjbC文件时, XCode会提示创建XXX-Bridging-Header.h文件, 创建之 在创 ...

  7. void (*f(int, void (*)(int)))(int) 函数解析 转

    今天与几个同学看到了一个函数指针定义: void (*f(int, void (*)(int)))(int) 以前在C trap pit fails里面见过,但是文章里面介绍的很详细,但是往往使初学者 ...

  8. HTML+JS版本的俄罗斯方块

    <!doctype html><html><head></head><body> <div id="box" st ...

  9. 从零开始学习MySQL3---数据库的基本操作

    创建数据库 MySQL安装完成后,将会在其Data目录下自动创建几个必需的数据库 可以用  SHOW DATABASES: 来查看当前存在的数据库 创建数据库是在系统磁盘上划分一块区域用于数据的存储和 ...

  10. spoj 4487. Can you answer these queries VI (gss6) splay 常数优化

    4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) in ...