#include <iostream> #include <vector> #include <algorithm> using std::cin, std::cout, std::endl; using std::min, std::max; using std::vector; int main() { cin.tie(0)->sync_with_stdio(false); int n; cin >> n; vector<int> occ(n + 1, 0); int h = 0, l = n + 1, k; for (int i = 0; i < n; i++) { cin >> k; h = max(h, k); l = min(l, k); occ[k]++; } vector<int> tab; tab.reserve(h - l + 1); for (int i = l; i <= h; ++i) if (occ[i] > 0) tab.push_back(occ[i]); std::sort(tab.begin(), tab.end()); int lp = 0, hp = tab.size() - 1; int ops = 0; while (lp < hp) { ops++; int cnt = tab[hp] - 1; while (cnt > 0 && lp < hp && tab[lp] <= cnt) { cnt -= tab[lp]; lp++; } tab[lp] -= cnt; hp--; } if (lp == hp) { ops++; } cout << ops << 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 | #include <iostream> #include <vector> #include <algorithm> using std::cin, std::cout, std::endl; using std::min, std::max; using std::vector; int main() { cin.tie(0)->sync_with_stdio(false); int n; cin >> n; vector<int> occ(n + 1, 0); int h = 0, l = n + 1, k; for (int i = 0; i < n; i++) { cin >> k; h = max(h, k); l = min(l, k); occ[k]++; } vector<int> tab; tab.reserve(h - l + 1); for (int i = l; i <= h; ++i) if (occ[i] > 0) tab.push_back(occ[i]); std::sort(tab.begin(), tab.end()); int lp = 0, hp = tab.size() - 1; int ops = 0; while (lp < hp) { ops++; int cnt = tab[hp] - 1; while (cnt > 0 && lp < hp && tab[lp] <= cnt) { cnt -= tab[lp]; lp++; } tab[lp] -= cnt; hp--; } if (lp == hp) { ops++; } cout << ops << endl; return 0; } |