#include <iostream> #include <vector> #include <set> namespace { using std::ios_base, std::cin; using std::cout, std::vector, std::multiset; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // Przygotowanie danych size_t n, m; cin >> n; vector<size_t> raw_cnts(n, 0); for (size_t k = 0; k < n; ++k) { cin >> m; ++raw_cnts[m - 1]; } multiset<size_t> cnts; for (auto const & c : raw_cnts) if (c != 0) cnts.insert(c); // Obliczenie wyniku size_t elem = 0, prev_min = 0; auto it_min = cnts.begin(); auto it_max = cnts.rbegin(); size_t i_min = 0, i_max = cnts.size() - 1; while (i_min < i_max) { if (*it_max > elem + *it_min - prev_min) { elem += *it_min; prev_min = 0; ++it_min; ++i_min; } else { prev_min = *it_max - elem - 1; elem = 0; ++it_max; --i_max; } } cout << cnts.size() - i_max << '\n'; }
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 | #include <iostream> #include <vector> #include <set> namespace { using std::ios_base, std::cin; using std::cout, std::vector, std::multiset; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // Przygotowanie danych size_t n, m; cin >> n; vector<size_t> raw_cnts(n, 0); for (size_t k = 0; k < n; ++k) { cin >> m; ++raw_cnts[m - 1]; } multiset<size_t> cnts; for (auto const & c : raw_cnts) if (c != 0) cnts.insert(c); // Obliczenie wyniku size_t elem = 0, prev_min = 0; auto it_min = cnts.begin(); auto it_max = cnts.rbegin(); size_t i_min = 0, i_max = cnts.size() - 1; while (i_min < i_max) { if (*it_max > elem + *it_min - prev_min) { elem += *it_min; prev_min = 0; ++it_min; ++i_min; } else { prev_min = *it_max - elem - 1; elem = 0; ++it_max; --i_max; } } cout << cnts.size() - i_max << '\n'; } |