You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 120;
int a[N], b[N];
const int INF = 0x3f3f3f3f;
/*
3
2 6 7
*/
int n, sum, avg;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
a[i + n] = a[i]; // 破环成链, 0 1 2-->a[3]=a[0],a[4]=a[1],a[5]=a[2]
}
avg = sum / n; // 平均数
int mi = INF; // 最少次数,给初始值最大
for (int i = 0; i < n; i++) { // 按每个石子堆为起点
for (int j = 0; j < n; j++) b[j] = a[i + j]; // 从原始的破环成链数组中拷贝出来现在的临时数组
int cnt = 0;
for (int j = 0; j < n - 1; j++) {
cnt += abs(b[j] - avg); // 需要调整几个石子到下一个石子堆
b[j + 1] += b[j] - avg; // 下一个石子堆接着这些石子
b[j] = avg; // j调整为平均值,其实这句没有发挥作用,可省略
}
// 以第i个石子为起点的讨论结束判断是不是我们需要的最优解
mi = min(mi, cnt);
}
cout << mi << endl;
return 0;
}