33 lines
870 B
C++
33 lines
870 B
C++
|
|
#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;
|
|||
|
|
}
|