洛谷 P4016 负载平衡问题
题目:
- 裸题。转链接
题解:
- 因为是裸题就没必要放题目了。模型是环形均分纸牌。以前做过几道类似的题。有详细讲解。链接
#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 105
using namespace std;
int n, avg, mid, ans;
int a[N], c[N];
int main()
{
cin >> n, mid = (n + 1) / 2;
for(int i = 1; i <= n; i++)
cin >> a[i], avg += a[i];
avg /= n;
for(int i = 1; i <= n; i++)
{
a[i] -= avg;
c[i] = c[i - 1] + a[i];
}
sort(c + 1, c + 1 + n);
for(int i = 1; i <= n; i++) ans += abs(c[i] - c[mid]);
cout << ans;
return 0;
}