#include <iostream> #include <algorithm> #include <map> #include <vector> using namespace std; struct Val { int all; int initial; }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; cin >> n; map<int, Val> freq; vector<pair<int, int>> lu; int x; for (int i = 0; i < n; ++i) { cin >> x; freq[x] = {freq[x].all + 1, freq[x].initial + 1}; } for (auto it = freq.cbegin(); it != freq.cend(); ++it) { lu.push_back({it->second.initial, it->first}); } sort(lu.begin(), lu.end(), greater<>()); for (auto it = lu.cbegin(); it != lu.cend(); ++it) { for (auto it2 = lu.rbegin(); it2 != lu.rend();) { if (freq[it->second].initial <= (freq[it->second].all + 1) / 2) { break; } int oiledodac = std::min((freq[it->second].initial * 2 - 1) - freq[it->second].all, freq[it2->second].all); freq[it->second] = {freq[it->second].all + oiledodac, freq[it->second].initial}; freq[it2->second] = {freq[it2->second].all - oiledodac, freq[it2->second].initial}; if (freq[it2->second].all == 0) { freq.erase(it2->second); auto tempIter = lu.erase(--it2.base()); it2 = vector<pair<int, int>>::reverse_iterator(tempIter); } else { break; } } } cout << freq.size() << "\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 | #include <iostream> #include <algorithm> #include <map> #include <vector> using namespace std; struct Val { int all; int initial; }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; cin >> n; map<int, Val> freq; vector<pair<int, int>> lu; int x; for (int i = 0; i < n; ++i) { cin >> x; freq[x] = {freq[x].all + 1, freq[x].initial + 1}; } for (auto it = freq.cbegin(); it != freq.cend(); ++it) { lu.push_back({it->second.initial, it->first}); } sort(lu.begin(), lu.end(), greater<>()); for (auto it = lu.cbegin(); it != lu.cend(); ++it) { for (auto it2 = lu.rbegin(); it2 != lu.rend();) { if (freq[it->second].initial <= (freq[it->second].all + 1) / 2) { break; } int oiledodac = std::min((freq[it->second].initial * 2 - 1) - freq[it->second].all, freq[it2->second].all); freq[it->second] = {freq[it->second].all + oiledodac, freq[it->second].initial}; freq[it2->second] = {freq[it2->second].all - oiledodac, freq[it2->second].initial}; if (freq[it2->second].all == 0) { freq.erase(it2->second); auto tempIter = lu.erase(--it2.base()); it2 = vector<pair<int, int>>::reverse_iterator(tempIter); } else { break; } } } cout << freq.size() << "\n"; return 0; } |