#include <algorithm> #include <cstdio> #include <cstring> #include <vector> const int NN = 500500; int D[NN]; int N, c; bool DEBUG = false; std::vector<int> uses; int main() { memset(D, 0, sizeof(int) * NN); scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &c); ++D[c]; } for (int i = 1; i <= N; ++i) { if (DEBUG) printf("%d ", D[i]); if (D[i] > 0) { uses.push_back(D[i]); } } if (DEBUG) puts("X"); sort(uses.begin(), uses.end()); if (DEBUG) { for (const int &u : uses) printf("%d ", u); } if (DEBUG) puts(""); if (DEBUG) puts(""); int groups = 1; auto left = uses.begin(); int leftValue = *left; for (auto right = --uses.end(); left != right; --right) { int pool = *right; if (DEBUG) printf("pool: %d\n", pool); while (leftValue < pool) { pool -= leftValue; ++left; if (left == right) break; leftValue = *left; if (DEBUG) puts("eaten"); } leftValue -= (pool - 1); if (DEBUG) printf("leftValueLeft %d\n", leftValue); if (left == right) break; groups += 1; } printf("%d\n", groups); 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 68 69 | #include <algorithm> #include <cstdio> #include <cstring> #include <vector> const int NN = 500500; int D[NN]; int N, c; bool DEBUG = false; std::vector<int> uses; int main() { memset(D, 0, sizeof(int) * NN); scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", &c); ++D[c]; } for (int i = 1; i <= N; ++i) { if (DEBUG) printf("%d ", D[i]); if (D[i] > 0) { uses.push_back(D[i]); } } if (DEBUG) puts("X"); sort(uses.begin(), uses.end()); if (DEBUG) { for (const int &u : uses) printf("%d ", u); } if (DEBUG) puts(""); if (DEBUG) puts(""); int groups = 1; auto left = uses.begin(); int leftValue = *left; for (auto right = --uses.end(); left != right; --right) { int pool = *right; if (DEBUG) printf("pool: %d\n", pool); while (leftValue < pool) { pool -= leftValue; ++left; if (left == right) break; leftValue = *left; if (DEBUG) puts("eaten"); } leftValue -= (pool - 1); if (DEBUG) printf("leftValueLeft %d\n", leftValue); if (left == right) break; groups += 1; } printf("%d\n", groups); return 0; } |