#include <iostream> using namespace std; const int NMAX = 500000; int dc[NMAX] = {0}; void quick_sort(int *tab, int lewy, int prawy) { if (prawy <= lewy) return; int i = lewy - 1, j = prawy + 1, pivot = tab[(lewy + prawy) / 2]; while (1) { while (pivot > tab[++i]) ; while (pivot < tab[--j]) ; if (i <= j) swap(tab[i], tab[j]); else break; } if (j > lewy) quick_sort(tab, lewy, j); if (i < prawy) quick_sort(tab, i, prawy); } int main() { int n, dig; cin >> n; for (int i = 0; i < n; i++) { cin >> dig; dc[dig]++; } if (n == 1) { cout << 1 << endl; } else { quick_sort(dc, 1, n); int j = n; int len = n; int min = 0; do { min++; int seqLen = dc[j] * 2 - 1; len -= seqLen; j--; } while (len > 0); cout << min << 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <iostream> using namespace std; const int NMAX = 500000; int dc[NMAX] = {0}; void quick_sort(int *tab, int lewy, int prawy) { if (prawy <= lewy) return; int i = lewy - 1, j = prawy + 1, pivot = tab[(lewy + prawy) / 2]; while (1) { while (pivot > tab[++i]) ; while (pivot < tab[--j]) ; if (i <= j) swap(tab[i], tab[j]); else break; } if (j > lewy) quick_sort(tab, lewy, j); if (i < prawy) quick_sort(tab, i, prawy); } int main() { int n, dig; cin >> n; for (int i = 0; i < n; i++) { cin >> dig; dc[dig]++; } if (n == 1) { cout << 1 << endl; } else { quick_sort(dc, 1, n); int j = n; int len = n; int min = 0; do { min++; int seqLen = dc[j] * 2 - 1; len -= seqLen; j--; } while (len > 0); cout << min << endl; } return 0; } |