#include<iostream> #include<map> #include<algorithm> #include<vector> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int n, x; int res = 0; std::cin >> n; std::map<int, int> m; for (int i = 0; i < n; i++) { std::cin >> x; m[x]++; } //std::cout << m.size(); std::map<int, int>::iterator itr; std::vector<int> w(m.size()); int i = 0; for (itr = m.begin(); itr != m.end(); itr++) { w[i] = (*itr).second; i++; } std::sort(w.begin(), w.end()); int j = 0; i = w.size() - 1; int a = 0, b = 0; while (true) { if (j == i + 1) { if (a < b) res++; std::cout << res; exit(0); } if (a <= b) { a += w[i]; i--; a--; res++; } else { b += w[j]; j++; } } 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 | #include<iostream> #include<map> #include<algorithm> #include<vector> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int n, x; int res = 0; std::cin >> n; std::map<int, int> m; for (int i = 0; i < n; i++) { std::cin >> x; m[x]++; } //std::cout << m.size(); std::map<int, int>::iterator itr; std::vector<int> w(m.size()); int i = 0; for (itr = m.begin(); itr != m.end(); itr++) { w[i] = (*itr).second; i++; } std::sort(w.begin(), w.end()); int j = 0; i = w.size() - 1; int a = 0, b = 0; while (true) { if (j == i + 1) { if (a < b) res++; std::cout << res; exit(0); } if (a <= b) { a += w[i]; i--; a--; res++; } else { b += w[j]; j++; } } return 0; } |