#include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> int main() { int n; scanf("%d", &n); std::vector<int> occs; occs.resize(n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); occs[x - 1]++; } std::sort(occs.begin(), occs.end()); int taken = 0; int components = 0; for (; taken < n; components++) { taken += 2 * occs[n - 1 - components] - 1; } printf("%d\n", components); 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 | #include <cstdio> #include <cstdlib> #include <algorithm> #include <vector> int main() { int n; scanf("%d", &n); std::vector<int> occs; occs.resize(n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); occs[x - 1]++; } std::sort(occs.begin(), occs.end()); int taken = 0; int components = 0; for (; taken < n; components++) { taken += 2 * occs[n - 1 - components] - 1; } printf("%d\n", components); return 0; } |