Files
python/TangDou/Mkx/2024J2/1_3.cpp

29 lines
765 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
// 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;
int main() {
// 进行一次排序
sort(a.begin(), a.end());
while (true) {
// 最小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;
// 重点来了!
swap(a[0], a[1]);
res += cha;
}
cout << "res=" << res << endl;
return 0;
}