#include <bits/stdc++.h> using namespace std; array<int,500001> a; long long n,m,i,j; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (i=0;i<n;i++){ cin >> m; a[m]++; } make_heap(a.begin(),a.end()); sort_heap(a.begin(),a.end()); j=500000; i=0; while (n>0){ if (a[j]){ i++; n-=2*a[j]-1; } j--; } cout << i << '\n'; }
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 | #include <bits/stdc++.h> using namespace std; array<int,500001> a; long long n,m,i,j; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (i=0;i<n;i++){ cin >> m; a[m]++; } make_heap(a.begin(),a.end()); sort_heap(a.begin(),a.end()); j=500000; i=0; while (n>0){ if (a[j]){ i++; n-=2*a[j]-1; } j--; } cout << i << '\n'; } |