#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n],b[n+1]; for (int i=0; i<n+1; i++) b[i]=0; for (int i=0; i<n; i++) cin >> a[i],b[a[i]]++; sort(b,b+n+1); reverse(b,b+n+1); int i=0,j=0; while (b[j+1]!=0) j++; int ans=0; while (i<=j) { ans++; int p=b[i]; b[i]=0; i++; p--; while (b[j]<=p) b[j]=0,j--; if (p!=0) b[j]-=p; } cout << ans << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n],b[n+1]; for (int i=0; i<n+1; i++) b[i]=0; for (int i=0; i<n; i++) cin >> a[i],b[a[i]]++; sort(b,b+n+1); reverse(b,b+n+1); int i=0,j=0; while (b[j+1]!=0) j++; int ans=0; while (i<=j) { ans++; int p=b[i]; b[i]=0; i++; p--; while (b[j]<=p) b[j]=0,j--; if (p!=0) b[j]-=p; } cout << ans << endl; } |