解题思路:环形纸牌均分问题
C++代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
long long a[N], b[N], s[N], n;
int main()
{
cin >> n;
long long sum = 0, res = 0;
for (int i = 1; i <= n; i ++ )
{
cin >> a[i];
sum += a[i];
}
for (int i = 1; i <= n; i ++ )
{
b[i] = a[i] - sum / n;
s[i] = s[i - 1] + b[i];
}
sort(s + 1, s + n + 1);
for (int i = 1; i <= n; i ++ )
{
res += abs(s[i] - s[(n + 1) / 2]);
}
cout << res << endl;
return 0;
}