#include<iostream>
using namespace std;
int main() {
int n;
std::ios::sync_with_stdio(false);
cin >> n;
int numbers[201748];
int number;
int max = -9999;
for(int i = 0; i < 201748; i++)
numbers[i] = 0;
for(int i = 0; i < n; i++) {
cin >> number;
numbers[number]++;
if(number > max) max = number;
}
int x = 0;
int condition = max;
bool exit = true;
while(x < condition){
while(numbers[x] > 1) {
exit = false;
numbers[x+1] += 1;
numbers[x] -= 2;
}
if(x+1 >= condition) {
if(numbers[x+1] > 1) {
condition++;
}
}
x++;
}
int r = 0;
for(int i = 0; i <= condition; i++) {
if(numbers[i] != 0) r = i;
}
cout << r << endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include<iostream> using namespace std; int main() { int n; std::ios::sync_with_stdio(false); cin >> n; int numbers[201748]; int number; int max = -9999; for(int i = 0; i < 201748; i++) numbers[i] = 0; for(int i = 0; i < n; i++) { cin >> number; numbers[number]++; if(number > max) max = number; } int x = 0; int condition = max; bool exit = true; while(x < condition){ while(numbers[x] > 1) { exit = false; numbers[x+1] += 1; numbers[x] -= 2; } if(x+1 >= condition) { if(numbers[x+1] > 1) { condition++; } } x++; } int r = 0; for(int i = 0; i <= condition; i++) { if(numbers[i] != 0) r = i; } cout << r << endl; } |
English