#include <algorithm> #include <iostream> #include <iomanip> #include <map> #define REP(x,n) for (int x=0;x<(n);++x) #define VAR(x,n) __typeof(n) x = (n) #define FOREACH(x,c) for(VAR(x,(c).begin()); x != (c).end(); ++x) //#define __DEBUG #define MAX_N 500001 using namespace std; int n,i,m; map<int,int> occurrencesByNumber; int occurrencesOnly[MAX_N]; int solve() { cin >> n; REP(x,n) { cin>>i; ++occurrencesByNumber[i]; } FOREACH(it,occurrencesByNumber) { occurrencesOnly[m++] = it->second; } std::sort(occurrencesOnly, occurrencesOnly+m, greater<int>()); #ifdef __DEBUG REP(x,m) { cerr << occurrencesOnly[x]; } #endif int sequences = 0; int leftItems = n; for (int* occurrence = occurrencesOnly; leftItems > 0; ++occurrence) { ++sequences; leftItems -= ((*occurrence)<<1)-1; } return sequences; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << solve() << 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 | #include <algorithm> #include <iostream> #include <iomanip> #include <map> #define REP(x,n) for (int x=0;x<(n);++x) #define VAR(x,n) __typeof(n) x = (n) #define FOREACH(x,c) for(VAR(x,(c).begin()); x != (c).end(); ++x) //#define __DEBUG #define MAX_N 500001 using namespace std; int n,i,m; map<int,int> occurrencesByNumber; int occurrencesOnly[MAX_N]; int solve() { cin >> n; REP(x,n) { cin>>i; ++occurrencesByNumber[i]; } FOREACH(it,occurrencesByNumber) { occurrencesOnly[m++] = it->second; } std::sort(occurrencesOnly, occurrencesOnly+m, greater<int>()); #ifdef __DEBUG REP(x,m) { cerr << occurrencesOnly[x]; } #endif int sequences = 0; int leftItems = n; for (int* occurrence = occurrencesOnly; leftItems > 0; ++occurrence) { ++sequences; leftItems -= ((*occurrence)<<1)-1; } return sequences; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << solve() << endl; return 0; } |