#include <iostream>
#include <cstring>
using namespace std;
unsigned msb(unsigned x) {
if (x <= 1)
return 0;
unsigned result = 1;
x >>= 1;
while ((x >>= 1) != 0) {
result += 1;
}
return result;
}
int main() {
std::ios_base::sync_with_stdio(false);
int n;
cin >> n;
const int maxPower = 201718;
unsigned counts[maxPower + 1];
memset(counts, 0, sizeof(int) * (maxPower+1));
unsigned maxInput = 0;
for (int i = 0; i < n; ++i) {
int tmp;
cin >> tmp;
++counts[tmp];
if (tmp > maxInput)
maxInput = tmp;
}
for (int i = 1; i <= maxInput; ++i)
counts[i] += counts[i-1]/2;
cout << maxInput + msb(counts[maxInput]) << endl;
// for (int i = 0; i < 520; ++i)
// cout << i << "\t" << msb(i) << 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 | #include <iostream> #include <cstring> using namespace std; unsigned msb(unsigned x) { if (x <= 1) return 0; unsigned result = 1; x >>= 1; while ((x >>= 1) != 0) { result += 1; } return result; } int main() { std::ios_base::sync_with_stdio(false); int n; cin >> n; const int maxPower = 201718; unsigned counts[maxPower + 1]; memset(counts, 0, sizeof(int) * (maxPower+1)); unsigned maxInput = 0; for (int i = 0; i < n; ++i) { int tmp; cin >> tmp; ++counts[tmp]; if (tmp > maxInput) maxInput = tmp; } for (int i = 1; i <= maxInput; ++i) counts[i] += counts[i-1]/2; cout << maxInput + msb(counts[maxInput]) << endl; // for (int i = 0; i < 520; ++i) // cout << i << "\t" << msb(i) << endl; } |
English