#include <algorithm> #include <cstdio> #include <cstdlib> #include <vector> int main() { constexpr int max_n{ 1000000 }; constexpr int max_a{ 201718 }; int n; std::scanf("%d", &n); std::vector<int> a; a.resize(n); int actual_min_a{ max_a }; int actual_max_a{ 0 }; for (int i{ 0 }; i < n; ++i) { std::scanf("%d", &(a[i])); actual_min_a = std::min(actual_min_a, a[i]); actual_max_a = std::max(actual_max_a, a[i]); } std::vector<int> powers; powers.resize(actual_max_a + 1); for (int i{ 0 }; i < n; ++i) { ++powers[a[i]]; } int reminder{ 0 }; int final_power{ actual_min_a }; for (; final_power <= actual_max_a; ++final_power) { reminder = (powers[final_power] + reminder) / 2; } while (reminder > 0) { reminder /= 2; ++final_power; } --final_power; std::printf("%d\n", final_power); }
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 | #include <algorithm> #include <cstdio> #include <cstdlib> #include <vector> int main() { constexpr int max_n{ 1000000 }; constexpr int max_a{ 201718 }; int n; std::scanf("%d", &n); std::vector<int> a; a.resize(n); int actual_min_a{ max_a }; int actual_max_a{ 0 }; for (int i{ 0 }; i < n; ++i) { std::scanf("%d", &(a[i])); actual_min_a = std::min(actual_min_a, a[i]); actual_max_a = std::max(actual_max_a, a[i]); } std::vector<int> powers; powers.resize(actual_max_a + 1); for (int i{ 0 }; i < n; ++i) { ++powers[a[i]]; } int reminder{ 0 }; int final_power{ actual_min_a }; for (; final_power <= actual_max_a; ++final_power) { reminder = (powers[final_power] + reminder) / 2; } while (reminder > 0) { reminder /= 2; ++final_power; } --final_power; std::printf("%d\n", final_power); } |