#include <cstdio> #include <vector> #include <algorithm> #include <set> int main() { int n; scanf("%d", &n); std::vector<int> vec; for (int i = 0; i < n; i++) { int u; scanf("%d", &u); vec.push_back(u); } std::sort(vec.begin(), vec.end()); std::set<int> set; for (int i = 0; i < vec.size(); i++) { int coin = vec[i]; while (!set.empty()) { auto it = set.find(coin); if (it == set.end()) { break; } else { set.erase(it); coin++; } } set.insert(coin); } printf("%d\n", *std::prev(set.end())); }
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 | #include <cstdio> #include <vector> #include <algorithm> #include <set> int main() { int n; scanf("%d", &n); std::vector<int> vec; for (int i = 0; i < n; i++) { int u; scanf("%d", &u); vec.push_back(u); } std::sort(vec.begin(), vec.end()); std::set<int> set; for (int i = 0; i < vec.size(); i++) { int coin = vec[i]; while (!set.empty()) { auto it = set.find(coin); if (it == set.end()) { break; } else { set.erase(it); coin++; } } set.insert(coin); } printf("%d\n", *std::prev(set.end())); } |