#include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string.h> #include <set> #include <random> #include <queue> #include <map> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/pb_ds/assoc_container.hpp> using namespace std; // using namespace __gnu_pbds; // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("arch=icelake-server") constexpr int N = (int)2e5+11; // constexpr int A = (int)1e9+11; mt19937 rnd(time(nullptr)); void solve(){ int n; cin >> n; int cnt[n+1]; memset(cnt,0,sizeof cnt); for(int i = 0; i < n; i++){ int a; cin >> a; cnt[a]++; } multiset<int> st; for(int i = 1; i <= n; i++){ if(cnt[i]) st.insert(cnt[i]); } int sum = 0; int answer = 1; while(!st.empty()){ int x = *st.begin(); st.erase(st.begin()); // cerr << "x: " << x << "\n"; // cerr << "sum: " << sum << "\n"; if(st.empty()) { if(sum >= x) answer++; break; } if(sum + x >= *(--st.end())){ auto it = st.upper_bound(sum); int can_take = *it - sum - 1; int rem = max(0,x-can_take); // cerr << "end: " << (it == st.end()) << "\n"; // cerr << *it << "\n"; if(it != st.end()) st.erase(it); st.insert(rem); sum = 0; answer++; continue; } sum += x; } cout << answer << "\n"; return; } int main(){ ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); int tests = 1; // cin >> tests; for(int test = 1; test <= tests; test++){ solve(); } return 0; } /** * 10 6 2 3 6 1 7 2 7 7 3 1 2 2 3 3 6 6 7 7 7 1 2 2 2 3 1 5 2 2 1 3 4 1 2 2 3 4 1 1 1 2 9 4 6 3 8 4 5 7 9 5 1 1 1 1 1 2 2 9 3 2 2 7 8 7 3 6 8 2 2 3 3 6 7 7 8 8 2 2 1 2 2 1 2 2 2 2 {1 2} 2 2 2 1 1 2 2 **/
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 | #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string.h> #include <set> #include <random> #include <queue> #include <map> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/pb_ds/assoc_container.hpp> using namespace std; // using namespace __gnu_pbds; // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") // #pragma GCC target("arch=icelake-server") constexpr int N = (int)2e5+11; // constexpr int A = (int)1e9+11; mt19937 rnd(time(nullptr)); void solve(){ int n; cin >> n; int cnt[n+1]; memset(cnt,0,sizeof cnt); for(int i = 0; i < n; i++){ int a; cin >> a; cnt[a]++; } multiset<int> st; for(int i = 1; i <= n; i++){ if(cnt[i]) st.insert(cnt[i]); } int sum = 0; int answer = 1; while(!st.empty()){ int x = *st.begin(); st.erase(st.begin()); // cerr << "x: " << x << "\n"; // cerr << "sum: " << sum << "\n"; if(st.empty()) { if(sum >= x) answer++; break; } if(sum + x >= *(--st.end())){ auto it = st.upper_bound(sum); int can_take = *it - sum - 1; int rem = max(0,x-can_take); // cerr << "end: " << (it == st.end()) << "\n"; // cerr << *it << "\n"; if(it != st.end()) st.erase(it); st.insert(rem); sum = 0; answer++; continue; } sum += x; } cout << answer << "\n"; return; } int main(){ ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); int tests = 1; // cin >> tests; for(int test = 1; test <= tests; test++){ solve(); } return 0; } /** * 10 6 2 3 6 1 7 2 7 7 3 1 2 2 3 3 6 6 7 7 7 1 2 2 2 3 1 5 2 2 1 3 4 1 2 2 3 4 1 1 1 2 9 4 6 3 8 4 5 7 9 5 1 1 1 1 1 2 2 9 3 2 2 7 8 7 3 6 8 2 2 3 3 6 7 7 8 8 2 2 1 2 2 1 2 2 2 2 {1 2} 2 2 2 1 1 2 2 **/ |