#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 500'003; int nums[N]; vector<int> v; int main(){ int n, a; scanf("%d", &n); for(int i = 0 ; i < n ; i++){ scanf("%d", &a); nums[a]++; } for(int i = 0 ; i < N ; i++){ if(nums[i] != 0) v.push_back(nums[i]); } sort(v.begin(), v.end()); int cnt = 0, it = 0, ans = 0, less; for(int i = v.size() - 1 ; i >= 0 ; i--){ if(cnt == 0 && v[i] > 0){ ans++; cnt = v[i] - 1; v[i] = 0; while(cnt > 0 && it <= i){ less = min(cnt, v[it]); cnt -= less; v[it] -= less; if(v[it] == 0) it++; } } } printf("%d\n", ans); }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 500'003; int nums[N]; vector<int> v; int main(){ int n, a; scanf("%d", &n); for(int i = 0 ; i < n ; i++){ scanf("%d", &a); nums[a]++; } for(int i = 0 ; i < N ; i++){ if(nums[i] != 0) v.push_back(nums[i]); } sort(v.begin(), v.end()); int cnt = 0, it = 0, ans = 0, less; for(int i = v.size() - 1 ; i >= 0 ; i--){ if(cnt == 0 && v[i] > 0){ ans++; cnt = v[i] - 1; v[i] = 0; while(cnt > 0 && it <= i){ less = min(cnt, v[it]); cnt -= less; v[it] -= less; if(v[it] == 0) it++; } } } printf("%d\n", ans); } |