#include <iostream> #include <algorithm> #include <vector> using namespace std; class MoneyBox { private: std::vector<int> v; int max_val = 0; public: void push_value(int val, int x=1) { max_val = max(max_val, val); if ((max_val + 1) > v.size()) { v.resize(2 * (max_val + 1), 0); } v[val] += x; } void compress_values() { for (int i = 0; i <= max_val; ++i) { int curr_val = v[i]; int r = curr_val % 2; int c = curr_val / 2; v[i] = r; if(c > 0) { push_value(i + 1, c); } } } int get_max() { return max_val; } }; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; MoneyBox mb; while(n--) { int a; cin >> a; mb.push_value(a); } mb.compress_values(); cout << mb.get_max() << 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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; class MoneyBox { private: std::vector<int> v; int max_val = 0; public: void push_value(int val, int x=1) { max_val = max(max_val, val); if ((max_val + 1) > v.size()) { v.resize(2 * (max_val + 1), 0); } v[val] += x; } void compress_values() { for (int i = 0; i <= max_val; ++i) { int curr_val = v[i]; int r = curr_val % 2; int c = curr_val / 2; v[i] = r; if(c > 0) { push_value(i + 1, c); } } } int get_max() { return max_val; } }; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; MoneyBox mb; while(n--) { int a; cin >> a; mb.push_value(a); } mb.compress_values(); cout << mb.get_max() << endl; return 0; } |