数学/思维 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 ...
随机推荐
- 硬盘安装Win 7系统Windows 7 系统硬盘安装教程(图解)
目前,win 7的市场占有率即将超过XP成为了第一大系统,很多人也用上了win 7,你是不是也还是徘徊呢?是否因为XP用习惯了,或者是不会安装 win7呢?win7安装其实不麻烦,不管是什么系统,安装 ...
- [bzoj5379]Tree_dfs序_线段树_倍增lca
Tree bzoj-5379 题目大意:给定一棵$n$节点的树.支持:换根.把节点$u$和$v$的$lca$的子树加.询问$u$的子树和. 注释:$1\le n,q\le 3\times 10^5$. ...
- ZOJ3956 ZJU2017校赛(dp)
题意:给出n对(h,c) 记 sumh为选出的h的总和 sumc为选出的c的总和 你可以从中选出任意多对(可以不选) 使得 sumh^2-sumh*sumc-sumc^2 最大 输出最大值 输入 ...
- Ubuntu 16.04安装***-qt5
上一篇文章http://www.cnblogs.com/EasonJim/p/7133097.html中,第5步安装本地代理源服务器使用的是electron-ssr,发觉这个东西难配且难用,非常不建议 ...
- mvn解决jar包冲突
转自:http://blog.csdn.net/guanglihuan/article/details/50512855 对于Jar包冲突问题,我们开发人员经常都会有碰到,当我们使用一些jar包中的类 ...
- 智能眼镜技术科普:VR、AR、MR的区别
前段时间, 获得谷歌5亿美元融资的技术公司Magic Leap在WSJD展会中放出了一段实录视频,引起不小骚动.如今,也有媒体称他们为MR公司,那么VR.AR.MR之间到底有什么区别呢. VR.AR. ...
- Android清单文件具体解释(四) ---- backupAgent的使用方法
在<application>节点中有一个很重要的属性,那就是backupAgent.这里我们将它单独列出来,从基本含义,使用方法及其相关属性等方面来具体介绍一下. 1.backupAgen ...
- centos部署jenkins
1. 实验环境: 操作系统: CentOS Linux release 7.2.1511 (Core) 软件版本: jdk-8u60-linux-x64 apache-tomcat-9.0. ...
- ionic/cordova 真机调试
android下简单,连接手机后,直接: myProjectPath>ionic run android ios下比较麻烦点,要先装ios-deploy: sudo npm install - ...
- YTU 2543: 数字整除
2543: 数字整除 时间限制: 1 Sec 内存限制: 128 MB 提交: 33 解决: 8 题目描述 定理:把一个至少两位的正整数的个位数字去掉,再从余下的数中减去个位数的5倍.当且仅当差是 ...