#include <bits/stdc++.h> using namespace std; const size_t maxn = 500'000; array<int, maxn + 1> ile_razy = {}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { int element; cin >> element; ile_razy[element]++; } sort(ile_razy.begin(), ile_razy.end(), [](int a, int b) { return a > b; }); /* for (int i = 0; i < n; i++) { cout << ile_razy[i].first << " " << ile_razy[i].second << "\n"; } */ int index_gorny = 0, index_dolny = n - 1, dzielenia = 0; while (index_gorny <= index_dolny) { int pomiesci = ile_razy[index_gorny]*2 - 1; pomiesci -= ile_razy[index_gorny]; for (; index_dolny > index_gorny; index_dolny--) { if (ile_razy[index_dolny] <= pomiesci) { pomiesci -= ile_razy[index_dolny]; } else { ile_razy[index_dolny] -= pomiesci; break; } } index_gorny++; dzielenia++; } cout << dzielenia << "\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 | #include <bits/stdc++.h> using namespace std; const size_t maxn = 500'000; array<int, maxn + 1> ile_razy = {}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { int element; cin >> element; ile_razy[element]++; } sort(ile_razy.begin(), ile_razy.end(), [](int a, int b) { return a > b; }); /* for (int i = 0; i < n; i++) { cout << ile_razy[i].first << " " << ile_razy[i].second << "\n"; } */ int index_gorny = 0, index_dolny = n - 1, dzielenia = 0; while (index_gorny <= index_dolny) { int pomiesci = ile_razy[index_gorny]*2 - 1; pomiesci -= ile_razy[index_gorny]; for (; index_dolny > index_gorny; index_dolny--) { if (ile_razy[index_dolny] <= pomiesci) { pomiesci -= ile_razy[index_dolny]; } else { ile_razy[index_dolny] -= pomiesci; break; } } index_gorny++; dzielenia++; } cout << dzielenia << "\n"; return 0; } |