#include <bits/stdc++.h> #define pb push_back #define st first #define nd second using ll = long long; using ld = long double; using namespace std; const int N = 500005; int n; int zlicz[N]; vector <int> v; bool czy(int k) { int suma_liderow = 0; int suma_nieliderow = 0; for(int i = 0; i < k; i++) { suma_liderow += v[i]; suma_nieliderow += v[i] - 1; } if(suma_liderow + suma_nieliderow >= n) return 1; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for(int i = 0; i < n; i++) { int x; cin >> x; zlicz[x]++; } for(int i = 1; i <= n; i++) { if(zlicz[i]) { v.pb(zlicz[i]); } } sort(v.begin(), v.end(), greater()); int l = 1, p = n, s; while(l < p) { s = (l + p) >> 1; if(!czy(s)) { l = s + 1; } else p = s; } cout << l; 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 | #include <bits/stdc++.h> #define pb push_back #define st first #define nd second using ll = long long; using ld = long double; using namespace std; const int N = 500005; int n; int zlicz[N]; vector <int> v; bool czy(int k) { int suma_liderow = 0; int suma_nieliderow = 0; for(int i = 0; i < k; i++) { suma_liderow += v[i]; suma_nieliderow += v[i] - 1; } if(suma_liderow + suma_nieliderow >= n) return 1; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for(int i = 0; i < n; i++) { int x; cin >> x; zlicz[x]++; } for(int i = 1; i <= n; i++) { if(zlicz[i]) { v.pb(zlicz[i]); } } sort(v.begin(), v.end(), greater()); int l = 1, p = n, s; while(l < p) { s = (l + p) >> 1; if(!czy(s)) { l = s + 1; } else p = s; } cout << l; return 0; } |