#include <iostream> #include <map> #include <algorithm> using namespace std; int main() { int coins = 0; cin>>coins; int values[coins]; map<int, int> val; for (int i = 0; i < coins; i++) { cin>>values[i]; val[values[i]]++; } int max = 0; for_each(val.begin(), val.end(), [&val, &max](pair<const int, int>& p){ //value : amount int carry = 0; carry = p.second/2; if(carry != 0) val[p.first+1] += carry; if (p.first > max) max = p.first; }); cout<<max; 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 | #include <iostream> #include <map> #include <algorithm> using namespace std; int main() { int coins = 0; cin>>coins; int values[coins]; map<int, int> val; for (int i = 0; i < coins; i++) { cin>>values[i]; val[values[i]]++; } int max = 0; for_each(val.begin(), val.end(), [&val, &max](pair<const int, int>& p){ //value : amount int carry = 0; carry = p.second/2; if(carry != 0) val[p.first+1] += carry; if (p.first > max) max = p.first; }); cout<<max; return 0; } |