#include <iostream> #include <map> #include <vector> #include <algorithm> using namespace std; int main(){ int n; cin >> n; map<int, int> cnt; int v; for(int i = 0; i < n; i++){ cin >> v; if(cnt.count(v) > 0){ int& cur = cnt.at(v); cur++; } else cnt.insert({v, 1}); } std::vector<int> vals; for(auto it = cnt.begin(); it != cnt.end(); it++){ vals.push_back((*it).second); } sort(vals.begin(), vals.end()); auto l = vals.begin(), r = vals.end(); r--; int ans = 0; while(l <= r){ ans++; int c = *r; while(l != r && c > 1) { if(*l < c) { c -= *l; l++; } else { *l -= c - 1; c = 1; } } r--; } cout << ans; 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 | #include <iostream> #include <map> #include <vector> #include <algorithm> using namespace std; int main(){ int n; cin >> n; map<int, int> cnt; int v; for(int i = 0; i < n; i++){ cin >> v; if(cnt.count(v) > 0){ int& cur = cnt.at(v); cur++; } else cnt.insert({v, 1}); } std::vector<int> vals; for(auto it = cnt.begin(); it != cnt.end(); it++){ vals.push_back((*it).second); } sort(vals.begin(), vals.end()); auto l = vals.begin(), r = vals.end(); r--; int ans = 0; while(l <= r){ ans++; int c = *r; while(l != r && c > 1) { if(*l < c) { c -= *l; l++; } else { *l -= c - 1; c = 1; } } r--; } cout << ans; return 0; } |