// #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; int main() { FAST int n; cin >> n; int tmp; unordered_map<int,int> occ; for (int i = 0 ; i<n ; i++) { cin >> tmp; occ[tmp]++; } vector<int> occCounts; for (auto& it: occ) { occCounts.push_back(it.second); } if (occCounts.size() == n) { cout << n; return 0; } sort(occCounts.rbegin(), occCounts.rend()); int total = 0; for (int i = 0 ;i < occCounts.size(); i++) { if (occCounts[i] == 1) { cout << i+(n-total); return 0; } total += occCounts[i] + occCounts[i]-1; if (total >=n) { cout << i+1 ; return 0; } } 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 | // #include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; int main() { FAST int n; cin >> n; int tmp; unordered_map<int,int> occ; for (int i = 0 ; i<n ; i++) { cin >> tmp; occ[tmp]++; } vector<int> occCounts; for (auto& it: occ) { occCounts.push_back(it.second); } if (occCounts.size() == n) { cout << n; return 0; } sort(occCounts.rbegin(), occCounts.rend()); int total = 0; for (int i = 0 ;i < occCounts.size(); i++) { if (occCounts[i] == 1) { cout << i+(n-total); return 0; } total += occCounts[i] + occCounts[i]-1; if (total >=n) { cout << i+1 ; return 0; } } return 0; } |