题目传送门

 /*
假设x1为1号给n号的金币数(逆时针),下面类似
a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[1] - m,下面类似
a[2] - x2 + x3 = m ,x3 = m + x2 - a[2] = m + (m + x1 - a[1]) - a[2] = 2 * m + x1 - a[1] - a[2] = x1 - c2;
a[3] - x3 + x4 = m ,x4 = m + x3 - a[3] =............................= 3 * m + x1 - a[1] - a[2] - a[3] = x1 - c3;
而我们求得是|x1|+|x2|+|x3|+....+|xn|
把上边的公式带入,每一项都会变成|x1 - ci|的形式,那就变成了:在数轴上有n个点,求到他们的距离和最小的点是谁?
然后结论是x1 = ci的中位数。
中位数证明:http://blog.csdn.net/zhengnanlee/article/details/8915098
*/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
int a[MAXN], c[MAXN]; int main(void) //UVA 11300 Spreading the Wealth
{
//freopen ("UVA_11300.in", "r", stdin); int n;
while (scanf ("%d", &n) == )
{
long long sum = ; int ave = ;
for (int i=; i<=n; ++i)
{
scanf ("%d", &a[i]);
sum += a[i];
}
ave = sum / n;
c[] = ;
for (int i=; i<=n; ++i)
{
c[i] = c[i-] + a[i] - ave;
} sort (c+, c++n);
int x = c[n/]; long long ans = ;
for (int i=; i<=n; ++i) ans += abs (x - c[i]); printf ("%lld\n", ans);
} return ;
}

数学/思维 UVA 11300 Spreading the Wealth的更多相关文章

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

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

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

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

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

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

  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. 【思维】UVA 11300 Spreading the Wealth

    题目大意 vjudge链接 有n个人围圆桌而坐,每个人有Ai个金币,每个人可以给左右相邻的人一些金币. 若使得最终所有人金币数相等,求最小金币转移数. 数据范围 n<1000001 样例输入 3 ...

  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. [K/3Cloud] 创建一个单据转换插件

    概念: 创建一个业务单据转换插件,在单据转换的各个时点干预单据转换的相关逻辑控制. 示例: 新建一个类,继承自单据转换插件基类Kingdee.BOS.Core.Metadata.ConvertElem ...

  2. Jquery EasyUI动态生成Tab

    function addTab(title, url) { if ($('#tt').tabs('exists', title)) { $('#tt').tabs('select', title); ...

  3. CF601D:Acyclic Organic Compounds

    给n<=300000的树,每个点上有一个字母,一个点的权值为:从该点出发向下走到任意节点停下形成的不同字符串的数量,问最大权值. 题目本身还有一些奇怪要求在此忽略.. Trie合并的模板题. # ...

  4. msp430入门编程02

    msp430单片机最小系统 msp430入门学习 msp430入门编程

  5. Ajax核心知识(2)

    对于Ajax核心的东西需要在进行总结提炼一下: xmlHttp对象. 方法:xml.responseText获取后台传递到前台的数据,经常性的使用var object=xml.responseText ...

  6. codevs——1006 等差数列

    1006 等差数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 给定n(1<=n< ...

  7. 18.10.7 POIN 模拟赛

    期望 :80+ +90+40=210+ 实际 :30+90+0=120 链接:https://www.nowcoder.com/acm/contest/175/A来源:牛客网 时间限制:C/C++ 1 ...

  8. 【CV论文阅读】Image Captioning 总结

    初次接触Captioning的问题,第一印象就是Andrej Karpathy好聪明.主要从他的两篇文章开始入门,<Deep Fragment Embeddings for Bidirectio ...

  9. C#程序如何把窗体文件从从一个项目中复制到另一个项目

    一个窗体有三个文件,全部拷贝到新的项目中   在新的项目中点击显示所有文件,然后右击导入的文件,点击包括在项目中,会自动修改颜色(此时还没有被识别为窗体)   重启这个项目,三个文件已经被识别出来了 ...

  10. react新特性 react hooks

    本文介绍的是react新特性react hooks,本文面向的是有一定react开发经验的小伙伴,如果你对react还不是很熟悉的话我建议你先学习react并多多联系. 首先我们都知道react有3种 ...