题目链接: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. Android学习4—短信发送器的实现

    界面预览: 由图中可以看出,此APP需要的组件有:两个TextView,一个用于显示手机号码的标题,另一个用于显示短信内容的标题.                                    ...

  2. CentOS7 开关防火墙

    systemctl start firewalld.service#启动firewallsystemctl stop firewalld.service#停止firewallsystemctl dis ...

  3. sql server 数据库基础学习心得 思维导图

  4. python cookielib

    # HttpClient.py is written by [xqin]: https://github.com/xqin/SmartQQ-for-Raspberry-Piimport cookiel ...

  5. Python 手册——开胃菜

    如果你写过大规模的Shell脚本,应该会有过这样的体会:你还非常想再加一些别的功能进去,但它已经太大. 太慢.太复杂了:或者这个功能需要调用一个系统函数,或者它只适合通过C来调用……通常这些问题还不足 ...

  6. CSS样式的优先级

    1.相同权值情况下,CSS样式的优先级总结来说,就是--就近原则(离被设置元素越近优先级别越高): 内联样式表(标签内部)> 嵌入样式表(当前文件中)> 外部样式表(外部文件中). 2.权 ...

  7. C语言之 短路原则

    a=0;b=1 c=a&&(b=3) 最终c=0; b=1 因为从左至右进行时,若遇到运算符左边的操作数是 0(逻辑假),则停止运算. a=1;b=1;c=0; d=a||b||(c= ...

  8. javascript日用代码集合(一)

    获取url参数 function get_url_param(name){ var reg = new RegExp("(^|&)" + name + "=([^ ...

  9. PHP 之 Laravel 框架安装及相关开源软件

    Laravel 被称为简洁.优雅的PHP开发框架,但第一次接触此框架的人有不少都卡在了安装上,其实在 Linux 下只需要很简单的几步就可以搞定,这里我们以 CentOS 下 PHP + Nginx ...

  10. 【转】MFC界面更新实现方法

    原文网址:http://www.cnblogs.com/skywatcher/p/3572311.html 1.更新窗口 即采用UpdateWindow()函数立即发送WM_PAINT消息更新整个窗口 ...