#include <iostream> #include <algorithm> #include <map> using namespace std; map<int, int> bankNoteCountMap; void updateMap(int bank_note, int value){ auto it = bankNoteCountMap.find(bank_note); if(it == bankNoteCountMap.end()){ bankNoteCountMap.emplace(bank_note, value); }else{ it->second+=value; } } int findBiggestbankNote(){ int result = 0; while(!bankNoteCountMap.empty()){ auto smallest = bankNoteCountMap.begin(); result = max(result, smallest->first); if(smallest->second / 2 > 0) { updateMap(smallest->first+1, smallest->second / 2); } bankNoteCountMap.erase(smallest); } return result; } int main() { int n; cin >> n; for(int i=0; i < n; ++i){ int bank_note; cin >> bank_note; updateMap(bank_note, 1); } cout << findBiggestbankNote()<< 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 51 52 53 | #include <iostream> #include <algorithm> #include <map> using namespace std; map<int, int> bankNoteCountMap; void updateMap(int bank_note, int value){ auto it = bankNoteCountMap.find(bank_note); if(it == bankNoteCountMap.end()){ bankNoteCountMap.emplace(bank_note, value); }else{ it->second+=value; } } int findBiggestbankNote(){ int result = 0; while(!bankNoteCountMap.empty()){ auto smallest = bankNoteCountMap.begin(); result = max(result, smallest->first); if(smallest->second / 2 > 0) { updateMap(smallest->first+1, smallest->second / 2); } bankNoteCountMap.erase(smallest); } return result; } int main() { int n; cin >> n; for(int i=0; i < n; ++i){ int bank_note; cin >> bank_note; updateMap(bank_note, 1); } cout << findBiggestbankNote()<< endl; return 0; } |