Files
python/TangDou/LanQiaoBei/ZhongGaoJi/LanQiao14STEMA202301/BC3.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

38 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 120;
int a[N], b[N];
const int INF = 0x3f3f3f3f;
/*
5
5 7 5 5 3 | 5 7 5 5 3
答案:4
*/
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]; // 破环成链
}
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;
}