#include <bits/stdc++.h>
using namespace std;
multiset <int> liderzy;
int ile[500007];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, x;
cin >> n;
for (int i = 0; i < n; ++i){
cin >> x;
ile[x]++;
}
for (int i = 0; i <= n; ++i){
if (ile[i] > 0){
liderzy.insert(-ile[i]);
// cout << ile[i] << " ";
}
}
// cout << "\n";
int wynik = 0;
while (!liderzy.empty()){
wynik++;
auto dozwolone = liderzy.begin();
int doz = *liderzy.begin();
doz *= -1;
//cout << "ROZPATRUJE:" << doz << "\n";
doz--;
liderzy.erase(dozwolone);
if (liderzy.empty()){
cout << wynik << "\n";
return 0;
}
//cout << "A";
while (doz > 0){
auto iter = liderzy.end();
iter--;
int odejmij = *iter;
odejmij *= -1;
// cout << odejmij << " " << doz << "\n";
if (odejmij <= doz){
liderzy.erase(iter);
//cout << "USUWAM:" << odejmij << "\n";
/*for (auto it = liderzy.begin(); it != liderzy.end(); ++it){
cout << *it << " ";
}*/
//cout << "\n";
doz -= odejmij;
if (liderzy.empty()){
cout << wynik << "\n";
return 0;
}
}
else{
liderzy.erase(iter);
liderzy.insert(doz - odejmij);
//cout << *iter << " ->> " << doz - odejmij << "\n";
/*for (auto it = liderzy.begin(); it != liderzy.end(); ++it){
cout << *it << " ";
}*/
//cout << "\n";
doz = 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <bits/stdc++.h> using namespace std; multiset <int> liderzy; int ile[500007]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, x; cin >> n; for (int i = 0; i < n; ++i){ cin >> x; ile[x]++; } for (int i = 0; i <= n; ++i){ if (ile[i] > 0){ liderzy.insert(-ile[i]); // cout << ile[i] << " "; } } // cout << "\n"; int wynik = 0; while (!liderzy.empty()){ wynik++; auto dozwolone = liderzy.begin(); int doz = *liderzy.begin(); doz *= -1; //cout << "ROZPATRUJE:" << doz << "\n"; doz--; liderzy.erase(dozwolone); if (liderzy.empty()){ cout << wynik << "\n"; return 0; } //cout << "A"; while (doz > 0){ auto iter = liderzy.end(); iter--; int odejmij = *iter; odejmij *= -1; // cout << odejmij << " " << doz << "\n"; if (odejmij <= doz){ liderzy.erase(iter); //cout << "USUWAM:" << odejmij << "\n"; /*for (auto it = liderzy.begin(); it != liderzy.end(); ++it){ cout << *it << " "; }*/ //cout << "\n"; doz -= odejmij; if (liderzy.empty()){ cout << wynik << "\n"; return 0; } } else{ liderzy.erase(iter); liderzy.insert(doz - odejmij); //cout << *iter << " ->> " << doz - odejmij << "\n"; /*for (auto it = liderzy.begin(); it != liderzy.end(); ++it){ cout << *it << " "; }*/ //cout << "\n"; doz = 0; } } } } |
English