UVA11300 Spreading the Wealth (数学推导+中位数)
来源:互联网 发布:高职大数据专业课程 编辑:程序博客网 时间:2024/05/16 07:06
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone
around a circular table. First, everyone has converted all of their properties to coins of equal value,
such that the total number of coins is divisible by the number of people in the village. Finally, each
person gives a number of coins to the person on his right and a number coins to the person on his left,
such that in the end, everyone has the same number of coins. Given the number of coins of each person,
compute the minimum number of coins that must be transferred using this method so that everyone
has the same number of coins.
Input
There is a number of inputs. Each input begins with
n
(
n<
1000001), the number of people in the
village.
n
lines follow, giving the number of coins of each person in the village, in counterclockwise
order around the table. The total number of coins will t inside an unsigned 64 bit integer.
Output
For each input, output the minimum number of coins that must be transferred on a single line.
SampleInput
3
100
100
100
4
1
2
5
4
SampleOutput
0
4
这道题数学味道太浓了。。
我们假设原来金币数为a1,a2,““`an.
最终的金币为这些数的平均值,设为A
xi表示i给i+1的金币个数
a1+xn-x1=A(每个人可以理解为被给了多少金币,给出了多少金币使得所有人的金币数平衡)
a2+x1-x2=A
a3+x2-x3=A
a4+x3-x4=A
……
an+x(n-1)-xn=A
用x1表示 x2,x3,…xn
x2=x1-(A-a2)
x3=x1-(2*A-a2-a3);
结果就是求|x1|+|x1-b1|+|x1-b2|+…+|x1-b[n-1]|的最小值,取中位数
这里的bi=A-ai,这里的A指的是平均数,ai表示第i个输入的数字。
#include<cstdio>#include<iostream>using namespace std;#include<cstring>#include<algorithm>typedef long long LL;const int maxn=1e6+5;int a[maxn];LL C[maxn];int main() { int n; while(~scanf("%d",&n)) { LL fl=0; for(int i=0; i<n; ++i) { scanf("%d",&a[i]),fl+=a[i]; } fl/=n; LL ans=0; C[0]=0LL; for(int i=1; i<n; ++i) { C[i]=C[i-1]+a[i]-fl; } sort(C,C+n); LL ave=C[n>>1]; for(int i=0;i<n;++i){ ans+=abs(ave-C[i]); } printf("%lld\n",ans); } return 0;}
- UVA11300 Spreading the Wealth (数学推导+中位数)
- uva11300 - Spreading the Wealth (中位数)
- UVA 11300 - Spreading the Wealth(数学推导+中位数)
- uva - 11300 - Spreading the Wealth(数学推导,中位数)
- UVa 11300 Spreading the Wealth (数学推导-中位数)
- UVa Spreading the Wealth-11300(数学推导+中位数)
- UVa11300 Spreading the Wealth(数学问题)
- UVA11300:Spreading the Wealth
- UVA11300 Spreading the Wealth
- uva11300 Spreading the Wealth
- Uva11300 - Spreading the Wealth
- UVa11300 - Spreading the Wealth
- Uva11300 - Spreading the Wealth
- uva11300 Spreading the Wealth
- UVA11300:Spreading the Wealth
- UVA11300:Spreading the Wealth
- uva 11300Spreading the Wealth(数学 中位数)
- UVa - 11300 - Spreading the Wealth ( 数学推导 )
- Search Insert Position
- 求二叉树的深度
- 接口
- Android开发之Android体系架构简介(二)
- C# 16进制与字符串、字节数组之间的转换
- UVA11300 Spreading the Wealth (数学推导+中位数)
- 使用css及简单的js实现酷炫的响应式slider
- 三大WEB服务器对比分析 (apache ,lighttpd,nginx)
- Mocapy++ in Heart
- Django快速学习搭建blog项目
- Android:EditText动态输入监听TextWatcher
- FFMPEG结构体分析:AVFormatContext
- C++拷贝构造函数、赋值运算符重载函数使用总结
- 我是如何利用一个只有500人的QQ通过人性的弱点来变现的