#include <algorithm> #include <iostream> //#include <pair> using namespace std; int N; pair<int, int> tab[500001]; int maxx = 0, x; bool check(pair<int, int> a, pair<int, int> b) { if (a.second > b.second) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; for (int i = 0; i < N; ++i) { cin >> x; if(x > maxx) maxx = x; if (tab[x].second == 0) { tab[x] = {x, 1}; } else { tab[x].second += 1; } } sort(tab, tab + maxx+1, check); int counter = 0; if(tab[0].second == 1){ for (int i = 0; i < maxx; ++i) { if(tab[i].second == 0){ break; } counter++; } } else{ int max1, index = maxx; for (int i = 0; i < maxx; ++i) { if(tab[i].second == 0){ break; } counter++; max1 = tab[i].second - 1; for (index; index > i; --index) { if(tab[index].second == 0) continue; if (tab[index].second < max1) { max1 -= tab[index].second; tab[index].second = 0; } else { tab[index].second -= max1; break; } } } } cout << counter; 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <algorithm> #include <iostream> //#include <pair> using namespace std; int N; pair<int, int> tab[500001]; int maxx = 0, x; bool check(pair<int, int> a, pair<int, int> b) { if (a.second > b.second) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; for (int i = 0; i < N; ++i) { cin >> x; if(x > maxx) maxx = x; if (tab[x].second == 0) { tab[x] = {x, 1}; } else { tab[x].second += 1; } } sort(tab, tab + maxx+1, check); int counter = 0; if(tab[0].second == 1){ for (int i = 0; i < maxx; ++i) { if(tab[i].second == 0){ break; } counter++; } } else{ int max1, index = maxx; for (int i = 0; i < maxx; ++i) { if(tab[i].second == 0){ break; } counter++; max1 = tab[i].second - 1; for (index; index > i; --index) { if(tab[index].second == 0) continue; if (tab[index].second < max1) { max1 -= tab[index].second; tab[index].second = 0; } else { tab[index].second -= max1; break; } } } } cout << counter; return 0; } |