#include <iostream> #include <vector> #include <algorithm> #include <deque> using namespace std; typedef long long ll; const ll maxN = 5e5 + 3; int liczby[maxN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { int num; cin >> num; liczby[num]++; } vector<int> ile; for (int i = 0; i < maxN; i++) { if (liczby[i] != 0) { ile.push_back(liczby[i]); } } sort(ile.begin(), ile.end(), greater<int>()); deque<int> dq; for (int a : ile) { dq.push_back(a); } int r = 0; while (!dq.empty()) { int sum = 0; int c = dq.front(); //cout << c << " "; dq.pop_front(); while (sum + dq.back() < c && !dq.empty()) { //cout << dq.back() << " "; sum += dq.back(); dq.pop_back(); } if (!dq.empty()) { int v = dq.back(); dq.pop_back(); v = v - (c - sum - 1); //cout << v << " "; dq.push_back(v); } //cout << "\n"; r++; } cout << r << "\n"; 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include <iostream> #include <vector> #include <algorithm> #include <deque> using namespace std; typedef long long ll; const ll maxN = 5e5 + 3; int liczby[maxN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { int num; cin >> num; liczby[num]++; } vector<int> ile; for (int i = 0; i < maxN; i++) { if (liczby[i] != 0) { ile.push_back(liczby[i]); } } sort(ile.begin(), ile.end(), greater<int>()); deque<int> dq; for (int a : ile) { dq.push_back(a); } int r = 0; while (!dq.empty()) { int sum = 0; int c = dq.front(); //cout << c << " "; dq.pop_front(); while (sum + dq.back() < c && !dq.empty()) { //cout << dq.back() << " "; sum += dq.back(); dq.pop_back(); } if (!dq.empty()) { int v = dq.back(); dq.pop_back(); v = v - (c - sum - 1); //cout << v << " "; dq.push_back(v); } //cout << "\n"; r++; } cout << r << "\n"; return 0; } |