Description
有n个小朋友坐成一圈,每人有ai个糖果。每人只能给左右两人传递糖果。每人每次传递一个糖果代价为1。
Input
第一行一个正整数nn<=1'000'000,表示小朋友的个数.
接下来n行,每行一个整数ai,表示第i个小朋友得到的糖果的颗数.
Output
求使所有人获得均等糖果的最小代价。
Sample Input
4
1
2
5
4
1
2
5
4
Sample Output
4
题解
咳咳...[UVa 11300]Spreading the Wealth
//It is made by Awson on 2017.10.23
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
using namespace std;
const int N = ; LL n, a[N+], m; void work() {
scanf("%lld", &n);
for (int i = ; i <= n; i++) {
scanf("%lld", &a[i]); m += a[i];
}
m /= n;
for (int i = ; i <= n; i++) {
a[i] = a[i-]+a[i]-m;
}
sort(a+, a+n+);
LL mid = a[n>>], ans = ;
for (int i = ; i <= n; i++) ans += Abs(mid-a[i]);
printf("%lld\n", ans);
}
int main() {
work();
return ;
}