减去平均值,求前缀和,排序
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 1000010
int a[MAX_N], sum[MAX_N];
int main()
{
int n;
cin >> n;
sum[0] = 0;
long cnt = 0;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
cnt += a[i];
}
int age = cnt / n;
for(int i = 1; i <= n; i++)
{
a[i] -= age;
sum[i] = sum[i - 1] + a[i];
}
sort(sum + 1, sum + 1 + n);
int mid = (n + 1) >> 1;
long long ans = 0;
for(int i = 1; i <= n; i++)
{
ans += abs(sum[i] - sum[mid]);
}
cout << ans << endl;
return 0;
}
环形分牌【中位数】【前缀和】
最新推荐文章于 2022-09-22 00:13:20 发布