#include <bits/stdc++.h> #include <cstdint> using std::cin, std::cout; using std::string; using std::array; using std::vector; using i8 = int8_t; using u8 = uint8_t; using i16 = int16_t; using u16 = uint16_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; const char endl = '\n'; template<class num> inline num ceildiv(num a, num b) { return (a + b - 1) / b; } template<class num> inline num modulo(num i, num n) { //return value >= 0 const num k = i % n; return k < 0 ? k + n : k; } const int max_n = 500000 + 5; int main() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); int occurences[max_n]{}; int n; cin >> n; for (int i = 0; i < n; i++) { int input; cin >> input; occurences[input]++; } vector<int> occurence_count; // all non-zero elements of occurences[] for (int count : occurences) { if (count != 0) occurence_count.push_back(count); } std::ranges::sort(occurence_count); { int n = occurence_count.size(); int sets = 0; int left = -1; int right = n; int space = 0; while (left + 1 < right) { if (space > 0) { left++; space -= occurence_count[left]; } else { sets++; right--; space += occurence_count[right] - 1; } } if (space < 0) sets++; // int set = 0; // int space = 0; // for (int i = 0; i < n - set; i++) { // space -= occurence_count[i]; // while (space < 0) { // set++; // space += occurence_count[n - set] - 1; // if (i >= n - set) { // space = INT_MAX; // we already made space for o[i] by making a set for it // break; // } // } // } cout << sets; } cout << 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <bits/stdc++.h> #include <cstdint> using std::cin, std::cout; using std::string; using std::array; using std::vector; using i8 = int8_t; using u8 = uint8_t; using i16 = int16_t; using u16 = uint16_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; const char endl = '\n'; template<class num> inline num ceildiv(num a, num b) { return (a + b - 1) / b; } template<class num> inline num modulo(num i, num n) { //return value >= 0 const num k = i % n; return k < 0 ? k + n : k; } const int max_n = 500000 + 5; int main() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); int occurences[max_n]{}; int n; cin >> n; for (int i = 0; i < n; i++) { int input; cin >> input; occurences[input]++; } vector<int> occurence_count; // all non-zero elements of occurences[] for (int count : occurences) { if (count != 0) occurence_count.push_back(count); } std::ranges::sort(occurence_count); { int n = occurence_count.size(); int sets = 0; int left = -1; int right = n; int space = 0; while (left + 1 < right) { if (space > 0) { left++; space -= occurence_count[left]; } else { sets++; right--; space += occurence_count[right] - 1; } } if (space < 0) sets++; // int set = 0; // int space = 0; // for (int i = 0; i < n - set; i++) { // space -= occurence_count[i]; // while (space < 0) { // set++; // space += occurence_count[n - set] - 1; // if (i >= n - set) { // space = INT_MAX; // we already made space for o[i] by making a set for it // break; // } // } // } cout << sets; } cout << endl; return 0; } |