#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int max = 0;
int rest;
int result;
for (int i = 0; i < n; i++)
{
if (a[i] > max)
max = a[i];
}
int m = max + 20;
int count[m];
for (int i = 0; i < m; i++)
{
count[i] = 0;
}
for (int i = 0; i < n; i++)
{
count[a[i]] +=1;
}
for (int i = 0; i < m-1; i++)
{
if (count[i] != 0)
{
rest = count[i]/2;
count[i] = count[i]%2;
count[i+1] = count[i+1] + rest;
}
}
for (int i = m-1; i >= 0; i--)
{
if (count[i] == 1)
{
result = i;
break;
}
}
cout << result << endl;
return 0;
}
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 57 58 59 | #include <iostream> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int max = 0; int rest; int result; for (int i = 0; i < n; i++) { if (a[i] > max) max = a[i]; } int m = max + 20; int count[m]; for (int i = 0; i < m; i++) { count[i] = 0; } for (int i = 0; i < n; i++) { count[a[i]] +=1; } for (int i = 0; i < m-1; i++) { if (count[i] != 0) { rest = count[i]/2; count[i] = count[i]%2; count[i+1] = count[i+1] + rest; } } for (int i = m-1; i >= 0; i--) { if (count[i] == 1) { result = i; break; } } cout << result << endl; return 0; } |
English