#include <cstdio> #include <map> #include <cmath> using namespace std; template<typename T = int, typename U = int> U UpperPowerOfTwo(T a) { U result = 1; U index = 0; while (result < a) { result *= 2; index++; } if (result > a) { result /= 2; index--; } return index; } int main() { int n; scanf("%d", &n); map<int, int> numbers; for (int i = 0;i < n;++i) { int x; scanf("%d", &x); numbers[x] ++; } for (auto it = numbers.begin(); it != numbers.end();++it) { int number = it->first; int quantity = it->second; while (quantity > 1) { int powerIndex = UpperPowerOfTwo<int, int>(quantity); int power = 1 << powerIndex; quantity -= power; numbers[number + powerIndex] ++; } } printf("%d", numbers.rbegin()->first); 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 | #include <cstdio> #include <map> #include <cmath> using namespace std; template<typename T = int, typename U = int> U UpperPowerOfTwo(T a) { U result = 1; U index = 0; while (result < a) { result *= 2; index++; } if (result > a) { result /= 2; index--; } return index; } int main() { int n; scanf("%d", &n); map<int, int> numbers; for (int i = 0;i < n;++i) { int x; scanf("%d", &x); numbers[x] ++; } for (auto it = numbers.begin(); it != numbers.end();++it) { int number = it->first; int quantity = it->second; while (quantity > 1) { int powerIndex = UpperPowerOfTwo<int, int>(quantity); int power = 1 << powerIndex; quantity -= power; numbers[number + powerIndex] ++; } } printf("%d", numbers.rbegin()->first); return 0; } |