#include <iostream> #include <vector> #include <set> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<int>T(n + 1); for (int i = 0; i < n; i++) { int a; cin >> a; T[a]++; } multiset<int>S; for (auto& x : T) if (x) S.insert(x); int it = 0; while (S.size()) { auto IT = S.find(*S.rbegin()); int x = *IT; S.erase(IT); int rest = 0; while (S.size() && (x > rest + 1)) { int t = (*S.begin()) - 1; rest++; S.erase(S.begin()); if (t) S.insert(t); } it++; } cout << it << '\n'; 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 | #include <iostream> #include <vector> #include <set> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<int>T(n + 1); for (int i = 0; i < n; i++) { int a; cin >> a; T[a]++; } multiset<int>S; for (auto& x : T) if (x) S.insert(x); int it = 0; while (S.size()) { auto IT = S.find(*S.rbegin()); int x = *IT; S.erase(IT); int rest = 0; while (S.size() && (x > rest + 1)) { int t = (*S.begin()) - 1; rest++; S.erase(S.begin()); if (t) S.insert(t); } it++; } cout << it << '\n'; return 0; } |