题目链接: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. window FILES——windows文件管理相关实例

    C语言下有一套文件管理方案.C++语言下也有一套自己的文件管理方案.windows系统当然也有自己的一套文件管理方案啦.对于普通char类型为基础的字符使用哪种方案的解决办法都是一样的,但是对于宽字符 ...

  2. php 连接mysql数据库并显示数据 实例 转载

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  3. 读书笔记之 - javascript 设计模式 - 享元模式

    本章探讨另一种优化模式-享元模式,它最适合于解决因创建大量类似对象而累及性能的问题.这种模式在javascript中尤其有用,因为复杂的javascript代码很快就会用光浏览器的所有可用内存,通过把 ...

  4. Ext.String 方法

    1.Ext.String.htmlEncode(value); 编码字符串,对其中特殊字符进行转义 xt.String.htmlEncode("hello'world"); //& ...

  5. "Cannot convert value '0000-00-00' from column 2 to TIMESTAMP"mysql时间转换bug

    今天在项目中遇到这样的一个bug,Cannot convert value '0000-00-00' from column 2 to TIMESTAMP 仔细一查,经过http://blog.csd ...

  6. Hive的Metastore contains multiple versions

    hive 客户端报错:Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeExcepti ...

  7. 重启iis线程池和iis站点

    服务器监控. 一定时间内或者iis异常,就重启线程池和站点 一般重启站点没啥用.. 重启线程池 效果明显. 重启站点: /// <summary> /// 根据名字重启站点.(没重启线程池 ...

  8. 消息队列msmq

    http://q.cnblogs.com/q/26895/ 远程队列必须现在运程服务器上创建. 在 Windows Server 2008 上安装 IIS 服务和 MSMQ 功能后,系统会在 IIS  ...

  9. http server v0.1_http_server.c

    /**************************************************************** filename: http_server.c author: xx ...

  10. python 大文件以行为单位读取方式比对

    http://www.cnblogs.com/aicro/p/3371986.html 先前需要做一个使用python读取大文件(大于1G),并逐条存入内存进行处理的工作.做了很多的尝试,最终看到了如 ...