#include<bits/stdc++.h> using namespace std; vector<int> ile, tab; vector<vector<int>> kub; int n; int main(){ ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0); cin>>n; ile.assign(n + 1, 0); kub.assign(n + 1, vector<int>()); for(int i = 0, v; i < n; ++i){ cin>>v; ile[v]++; } for(int i = 1; i <= n; ++i){ kub[ile[i]].push_back(i); } for(int i = 1; i <= n; ++i){ for(int v : kub[i]) tab.push_back(v); } int left = n; int result = 0; while(left > 0){ result++; left -= ile[tab.back()] * 2 - 1; tab.pop_back(); } cout<<result<<endl; 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 | #include<bits/stdc++.h> using namespace std; vector<int> ile, tab; vector<vector<int>> kub; int n; int main(){ ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0); cin>>n; ile.assign(n + 1, 0); kub.assign(n + 1, vector<int>()); for(int i = 0, v; i < n; ++i){ cin>>v; ile[v]++; } for(int i = 1; i <= n; ++i){ kub[ile[i]].push_back(i); } for(int i = 1; i <= n; ++i){ for(int v : kub[i]) tab.push_back(v); } int left = n; int result = 0; while(left > 0){ result++; left -= ile[tab.back()] * 2 - 1; tab.pop_back(); } cout<<result<<endl; return 0; } |