#include <iostream>
#include <deque>
#include <algorithm>
constexpr int maxn = 5e5 + 7;
int n;
int zlicz[maxn];
std::deque<int> Q;
void input() {
scanf("%d", &n);
int x;
for(int i = 0; i < n; i++) {
scanf("%d", &x);
zlicz[x]++;
}
}
void wrzucNaQ() {
std::sort(zlicz + 1, zlicz + n + 1);
for(int i = 1; i <= n; i++)
if(zlicz[i])
Q.push_back(zlicz[i]);
}
int answer() {
int ans = 0;
while(!Q.empty()) {
int ile = Q.back() - 1;
Q.pop_back();
while(!Q.empty() && ile) {
if(ile >= Q.front()) {
ile -= Q.front();
Q.pop_front();
continue;
}
ile = Q.front() - ile;
Q.pop_front();
Q.push_front(ile);
ile = 0;
}
ans++;
}
return ans;
}
int main() {
input();
wrzucNaQ();
printf("%d", answer());
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 49 50 51 52 | #include <iostream> #include <deque> #include <algorithm> constexpr int maxn = 5e5 + 7; int n; int zlicz[maxn]; std::deque<int> Q; void input() { scanf("%d", &n); int x; for(int i = 0; i < n; i++) { scanf("%d", &x); zlicz[x]++; } } void wrzucNaQ() { std::sort(zlicz + 1, zlicz + n + 1); for(int i = 1; i <= n; i++) if(zlicz[i]) Q.push_back(zlicz[i]); } int answer() { int ans = 0; while(!Q.empty()) { int ile = Q.back() - 1; Q.pop_back(); while(!Q.empty() && ile) { if(ile >= Q.front()) { ile -= Q.front(); Q.pop_front(); continue; } ile = Q.front() - ile; Q.pop_front(); Q.push_front(ile); ile = 0; } ans++; } return ans; } int main() { input(); wrzucNaQ(); printf("%d", answer()); return 0; } |
English