数学/思维 UVA 11300 Spreading the Wealth
/*
假设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的更多相关文章
- UVA.11300 Spreading the Wealth (思维题 中位数模型)
UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...
- UVa 11300 Spreading the Wealth(有钱同使)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- uva 11300 - Spreading the Wealth(数论)
题目链接:uva 11300 - Spreading the Wealth 题目大意:有n个人坐在圆桌旁,每个人有一定的金币,金币的总数可以被n整除,现在每个人可以给左右的人一些金币,使得每个人手上的 ...
- UVA - 11300 Spreading the Wealth(数学题)
UVA - 11300 Spreading the Wealth [题目描述] 圆桌旁边坐着n个人,每个人有一定数量的金币,金币的总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金 ...
- UVA 11300 Spreading the Wealth (数学推导 中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- Uva 11300 Spreading the Wealth(递推,中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- Math - Uva 11300 Spreading the Wealth
Spreading the Wealth Problem's Link ---------------------------------------------------------------- ...
- 【思维】UVA 11300 Spreading the Wealth
题目大意 vjudge链接 有n个人围圆桌而坐,每个人有Ai个金币,每个人可以给左右相邻的人一些金币. 若使得最终所有人金币数相等,求最小金币转移数. 数据范围 n<1000001 样例输入 3 ...
- [ACM_几何] UVA 11300 Spreading the Wealth [分金币 左右给 最终相等 方程组 中位数]
Problem A Communist regime is trying to redistribute wealth in a village. They have have decided to ...
随机推荐
- 自己打断点走的struts流程&拦截器工作原理
①. 请求发送给 StrutsPrepareAndExecuteFilter ②. StrutsPrepareAndExecuteFilter 判定该请求是否是一个 Struts2 请 求(Actio ...
- SOJ 2785_Binary Partitions
[题意]将一个数用二进制数表示,求一共有多少种表示方法. [分析]思路一:完全背包 [代码] #include <iostream> #include <cstdio> #in ...
- 使用gdb调试python程序
参考文章:https://mozillazg.com/2017/07/debug-running-python-process-with-gdb.html https://blog.alswl.com ...
- LoadRunner脚本编写
性能測试project师要懂代码么?答案是必须的.好多測试员觉得在loadrunner中编写脚本非常难非常牛X ,主要是大多測试人员并未做过开发工作,大学的那点程序基础也忘记的几乎相同了. 还有非计算 ...
- vux picker
1.通过实验证明: PopupPicker = TransferDom + Popup + PopupHeader + Picker 2.代码 Picker.vue <!-- Picker 组件 ...
- hdu 1565 方格取数(1)(状态压缩dp)
方格取数(1) Time Limit: 10000/5000 MS (J ...
- 黑马day16 aptana插件的安装
aptana: eclipse或者myeclipse中的javaScript,html,css的代码提示功能非常差...因此我们选择了这个框架. aptana的安装步骤: 1.须要下载aptana的插 ...
- 六:二叉树中第k层节点个数与二叉树叶子节点个数
二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...
- HDUOJ Number Sequence 题目1005
/*Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- [DLX反复覆盖] poj 1084 Square Destroyer
题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...