#include<bits/stdc++.h> #define FOR(i,s,e) for(int i=(s); i<=(e); i++) #define FORD(i,s,e) for(int i=(s); i>=(e); i--) #define ALL(k) (k).begin(), (k).end() #define e1 first #define e2 second #define mp make_pair using namespace std; using LL=long long; using LD=long double; using PII=pair<int,int>; const int MAXN = 500111; int counts[MAXN]; int main(){ int n; scanf("%d", &n); FOR(i,1,n){ int a; scanf("%d",&a); counts[a]++; } vector<PII> count_pairs; FOR(i,1,n){ if (counts[i] > 0){ count_pairs.push_back(mp(counts[i], i)); } } sort(ALL(count_pairs)); reverse(ALL(count_pairs)); int lptr = 0, rptr = count_pairs.size() - 1; int minimum_subsequences = 0; while (lptr < rptr) { int quantity = count_pairs[lptr].e1 - 1; count_pairs[lptr].e1 = 0; minimum_subsequences += 1; while (rptr > lptr && quantity > 0) { int to_remove = min(quantity, count_pairs[rptr].e1); // printf("L %d R %d values %d %d, to_remove %d\n", lptr, rptr, count_pairs[lptr].e1, count_pairs[rptr].e1, to_remove); quantity -= to_remove; count_pairs[rptr].e1 -= to_remove; if (count_pairs[rptr].e1 == 0){ rptr--; } } lptr++; } // printf("Fin: L %d R %d count %d\n", lptr, rptr, count_pairs[lptr].e1); if (lptr == rptr && count_pairs[lptr].e1 > 0){ minimum_subsequences++; } printf("%d\n", minimum_subsequences); }
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 | #include<bits/stdc++.h> #define FOR(i,s,e) for(int i=(s); i<=(e); i++) #define FORD(i,s,e) for(int i=(s); i>=(e); i--) #define ALL(k) (k).begin(), (k).end() #define e1 first #define e2 second #define mp make_pair using namespace std; using LL=long long; using LD=long double; using PII=pair<int,int>; const int MAXN = 500111; int counts[MAXN]; int main(){ int n; scanf("%d", &n); FOR(i,1,n){ int a; scanf("%d",&a); counts[a]++; } vector<PII> count_pairs; FOR(i,1,n){ if (counts[i] > 0){ count_pairs.push_back(mp(counts[i], i)); } } sort(ALL(count_pairs)); reverse(ALL(count_pairs)); int lptr = 0, rptr = count_pairs.size() - 1; int minimum_subsequences = 0; while (lptr < rptr) { int quantity = count_pairs[lptr].e1 - 1; count_pairs[lptr].e1 = 0; minimum_subsequences += 1; while (rptr > lptr && quantity > 0) { int to_remove = min(quantity, count_pairs[rptr].e1); // printf("L %d R %d values %d %d, to_remove %d\n", lptr, rptr, count_pairs[lptr].e1, count_pairs[rptr].e1, to_remove); quantity -= to_remove; count_pairs[rptr].e1 -= to_remove; if (count_pairs[rptr].e1 == 0){ rptr--; } } lptr++; } // printf("Fin: L %d R %d count %d\n", lptr, rptr, count_pairs[lptr].e1); if (lptr == rptr && count_pairs[lptr].e1 > 0){ minimum_subsequences++; } printf("%d\n", minimum_subsequences); } |