#include<bits/stdc++.h> using namespace std; const int n_max = 5 * 1e5 + 7; int tab[n_max]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i = 0; i < n; i++){ int a; cin >> a; tab[a]++; } priority_queue<int> next_zone; for(int i = 0; i <= n; i++){ if(tab[i] != 0){ next_zone.push(tab[i]); } } int blocked = 0; int leaders = 0; while(blocked < n){ auto a = next_zone.top(); next_zone.pop(); blocked += a * 2 - 1; leaders++; } cout << leaders << '\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 | #include<bits/stdc++.h> using namespace std; const int n_max = 5 * 1e5 + 7; int tab[n_max]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i = 0; i < n; i++){ int a; cin >> a; tab[a]++; } priority_queue<int> next_zone; for(int i = 0; i <= n; i++){ if(tab[i] != 0){ next_zone.push(tab[i]); } } int blocked = 0; int leaders = 0; while(blocked < n){ auto a = next_zone.top(); next_zone.pop(); blocked += a * 2 - 1; leaders++; } cout << leaders << '\n'; return 0; } |