// Kacper Orszulak #ifndef DEBUG #define NDEBUG #endif #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vb = vector<bool>; using vvi = vector<vi>; using vvll = vector<vll>; #define st first #define nd second #define pb push_back #define rep(i, n) for (int i = 0; i < int(n); ++i) #define whole(x) x.begin(), x.end() [[maybe_unused]] constexpr int INF = 1e9+7; template<class T> bool minR(T &a, const T &b) { if (b < a) { a = b; return true; } else return false; } template<class T> bool maxR(T &a, const T &b) { if (b > a) { a = b; return true; } else return false; } void solve() { cerr << "SOL\n"; int n; cin >> n; vi cnter(n); rep(i, n) { int val; cin >> val; --val; ++cnter[val]; } sort(whole(cnter)); deque<int> nextCount; for (const int &e : cnter) nextCount.push_back(e); int sum = n; int result = 0; while (not nextCount.empty()) { if (nextCount.back() > sum/2) { ++result; break; } const int big = nextCount.back(); sum -= big; nextCount.pop_back(); assert(not nextCount.empty()); int smallerSum = 0; while (not nextCount.empty() and nextCount.front() + smallerSum < big) { const int small = nextCount.front(); smallerSum += small; sum -= small; nextCount.pop_front(); } if (not nextCount.empty() and smallerSum < big-1) { const int diff = big-1 - smallerSum; nextCount.front() -= diff; sum -= diff; assert(nextCount.front() > 0); smallerSum += diff; } ++result; } cout << result << '\n'; } int n; vi arr; // bool checkIfPossible(int result) { // do { // vb borders; // borders.reserve(n-1); // // rep(_, n-1 - (result-1)) // borders.pb(false); // rep(_, result-1) // borders.pb(true); // assert(borders.size() == n-1); // // do { // bool correct = true; // // for (int i = 0; i < n; ++i) { // // } // // if (correct) // return true; // } while (next_permutation(whole(borders))); // // } while (next_permutation(whole(arr))); // // return false; // } bool checkIfPossible(int toTakeMore, int usedMask = 0) { if (toTakeMore == 0) { if (__builtin_popcount(usedMask) == n) return true; else return false; } const int usedCnt = __builtin_popcount(usedMask); const int unusedCnt = n - usedCnt; for (int takeUnused = 0; takeUnused < (1 << unusedCnt); ++takeUnused) { const int subSetLen = __builtin_popcount(takeUnused); int greatestCount = 0; vi cnter(n, 0); int newUsedMask = usedMask; for (int i = 0, j = 0; i < unusedCnt; ++i, ++j) { while (usedMask & (1 << j)) ++j; assert(j < n); if (not (takeUnused & (1<<i))) continue; newUsedMask |= (1<<j); maxR(greatestCount, ++cnter[arr[j]-1]); } if (greatestCount > subSetLen/2) if (checkIfPossible(toTakeMore-1, newUsedMask)) return true; } return false; } void brute() { cerr << "BRUTE\n"; cin >> n; arr.resize(n); for (int &e : arr) { cin >> e; } sort(whole(arr)); int result = 1; for (; result <= n; ++result) { if (checkIfPossible(result)) { break; } } assert(result <= n); if (result > n) result = n; cout << result << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef SOL solve(); #elif defined BRUTE brute(); #else solve(); #endif }
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | // Kacper Orszulak #ifndef DEBUG #define NDEBUG #endif #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vb = vector<bool>; using vvi = vector<vi>; using vvll = vector<vll>; #define st first #define nd second #define pb push_back #define rep(i, n) for (int i = 0; i < int(n); ++i) #define whole(x) x.begin(), x.end() [[maybe_unused]] constexpr int INF = 1e9+7; template<class T> bool minR(T &a, const T &b) { if (b < a) { a = b; return true; } else return false; } template<class T> bool maxR(T &a, const T &b) { if (b > a) { a = b; return true; } else return false; } void solve() { cerr << "SOL\n"; int n; cin >> n; vi cnter(n); rep(i, n) { int val; cin >> val; --val; ++cnter[val]; } sort(whole(cnter)); deque<int> nextCount; for (const int &e : cnter) nextCount.push_back(e); int sum = n; int result = 0; while (not nextCount.empty()) { if (nextCount.back() > sum/2) { ++result; break; } const int big = nextCount.back(); sum -= big; nextCount.pop_back(); assert(not nextCount.empty()); int smallerSum = 0; while (not nextCount.empty() and nextCount.front() + smallerSum < big) { const int small = nextCount.front(); smallerSum += small; sum -= small; nextCount.pop_front(); } if (not nextCount.empty() and smallerSum < big-1) { const int diff = big-1 - smallerSum; nextCount.front() -= diff; sum -= diff; assert(nextCount.front() > 0); smallerSum += diff; } ++result; } cout << result << '\n'; } int n; vi arr; // bool checkIfPossible(int result) { // do { // vb borders; // borders.reserve(n-1); // // rep(_, n-1 - (result-1)) // borders.pb(false); // rep(_, result-1) // borders.pb(true); // assert(borders.size() == n-1); // // do { // bool correct = true; // // for (int i = 0; i < n; ++i) { // // } // // if (correct) // return true; // } while (next_permutation(whole(borders))); // // } while (next_permutation(whole(arr))); // // return false; // } bool checkIfPossible(int toTakeMore, int usedMask = 0) { if (toTakeMore == 0) { if (__builtin_popcount(usedMask) == n) return true; else return false; } const int usedCnt = __builtin_popcount(usedMask); const int unusedCnt = n - usedCnt; for (int takeUnused = 0; takeUnused < (1 << unusedCnt); ++takeUnused) { const int subSetLen = __builtin_popcount(takeUnused); int greatestCount = 0; vi cnter(n, 0); int newUsedMask = usedMask; for (int i = 0, j = 0; i < unusedCnt; ++i, ++j) { while (usedMask & (1 << j)) ++j; assert(j < n); if (not (takeUnused & (1<<i))) continue; newUsedMask |= (1<<j); maxR(greatestCount, ++cnter[arr[j]-1]); } if (greatestCount > subSetLen/2) if (checkIfPossible(toTakeMore-1, newUsedMask)) return true; } return false; } void brute() { cerr << "BRUTE\n"; cin >> n; arr.resize(n); for (int &e : arr) { cin >> e; } sort(whole(arr)); int result = 1; for (; result <= n; ++result) { if (checkIfPossible(result)) { break; } } assert(result <= n); if (result > n) result = n; cout << result << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef SOL solve(); #elif defined BRUTE brute(); #else solve(); #endif } |