AcWing 122. 15 糖果传递
原题链接
简单
作者:
QAQ_ning
,
2021-05-06 16:19:24
,
所有人可见
,
阅读 402
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
int n;
int a[N];
long long c[N];
long long sum;
int ave;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ )
{
scanf("%d", &a[i]);
sum += a[i];
}
ave = sum / n;
for (int i = n; i > 1; i -- )
c[i] = c[i + 1] - ave + a[i]; //推公式
c[1] = 0;
sort(c + 1, c + n + 1);
long long res;
for (int i = 1; i <= n; i ++ )
res += fabs(c[i] - c[n + 1 >> 1]);
printf("%lld", res);
return 0;
}