#include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; int main() { int numElements, num; map<int,int> count; cin >> numElements; for (int i=0; i<numElements; ++i) { cin >> num; ++count[num]; } vector<pair<int,int>> multiplicities; for (const auto & element : count) { multiplicities.push_back(make_pair(element.second, element.first)); } sort(multiplicities.begin(), multiplicities.end(), greater<pair<int,int>>()); int numSeq = {0}; for (const auto & element : multiplicities) { numElements -= 2 * element.first - 1; ++numSeq; if (numElements <= 0) break; } cout << numSeq << 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 | #include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; int main() { int numElements, num; map<int,int> count; cin >> numElements; for (int i=0; i<numElements; ++i) { cin >> num; ++count[num]; } vector<pair<int,int>> multiplicities; for (const auto & element : count) { multiplicities.push_back(make_pair(element.second, element.first)); } sort(multiplicities.begin(), multiplicities.end(), greater<pair<int,int>>()); int numSeq = {0}; for (const auto & element : multiplicities) { numElements -= 2 * element.first - 1; ++numSeq; if (numElements <= 0) break; } cout << numSeq << endl; return 0; } |