Files
python/TangDou/Mkx/2024J2/1_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

33 lines
870 B
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 = 1e6 + 10;
// vector<int> a = {2, 2, 2, 2};
// vector<int> a = {4, 2};
// vector<int> a = {5, 4, 2};
vector<int> a = {5, 4, 2, 7, 3, 4, 6, 7};
int n = a.size();
int res;
void print() {
// 输出
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << endl;
}
int main() {
while (true) {
// 由小到大排序
sort(a.begin(), a.end());//N*LogN
print();
// 最小a[0],次小a[1]
if (a[0] == 0) {
res += a[n - 1];
break;
}
// 利用最小与次小的关系,可以减少循环的次数
int cha = a[1] - a[0] + 1; // 比如:2 4,那么 2 3->2 2->2 1
for (int i = 1; i < n; i++) a[i] -= cha;
res += cha;
}
cout << "res=" << res << endl;
return 0;
}