#include <algorithm> #include <iostream> #include <unordered_map> #include <vector> using namespace std; int minimalNumberOfSequences(vector<int> &sequence) { unordered_map<int, int> counts; for (int num : sequence) { counts[num]++; } vector<pair<int, int>> freq; for (auto &p : counts) { freq.push_back({p.second, p.first}); } sort(freq.begin(), freq.end(), greater<pair<int, int>>()); int i = 0, j = freq.size() - 1, suma = 0; while (i < j) { suma = 0; for (int m = j; m >= i + 1; m--) { suma += freq[m].first; if (suma >= freq[i].first) { j = m; break; } } if (suma == (freq[i].first - 1)) { j--; } if (suma < (freq[i].first - 1)){ j--; } if (suma > (freq[i].first - 1)){ freq[j].first = suma - freq[i].first + 1; } i++; } if (i > j){ return i; } else { return i + 1; } } int main() { int n; cin >> n; vector<int> sequence(n); for (int i = 0; i < n; i++) { cin >> sequence[i]; } cout << minimalNumberOfSequences(sequence) << endl; 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 | #include <algorithm> #include <iostream> #include <unordered_map> #include <vector> using namespace std; int minimalNumberOfSequences(vector<int> &sequence) { unordered_map<int, int> counts; for (int num : sequence) { counts[num]++; } vector<pair<int, int>> freq; for (auto &p : counts) { freq.push_back({p.second, p.first}); } sort(freq.begin(), freq.end(), greater<pair<int, int>>()); int i = 0, j = freq.size() - 1, suma = 0; while (i < j) { suma = 0; for (int m = j; m >= i + 1; m--) { suma += freq[m].first; if (suma >= freq[i].first) { j = m; break; } } if (suma == (freq[i].first - 1)) { j--; } if (suma < (freq[i].first - 1)){ j--; } if (suma > (freq[i].first - 1)){ freq[j].first = suma - freq[i].first + 1; } i++; } if (i > j){ return i; } else { return i + 1; } } int main() { int n; cin >> n; vector<int> sequence(n); for (int i = 0; i < n; i++) { cin >> sequence[i]; } cout << minimalNumberOfSequences(sequence) << endl; return 0; } |