#include <algorithm> #include <cstdio> #include <vector> using namespace std; int main () { int n; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); sort(arr, arr + n); reverse(arr, arr + n); vector<int> counts; counts.push_back(1); for (int i = 1; i < n; i++) { if (arr[i] == arr[i - 1]) counts.back()++; else counts.push_back(1); } sort(counts.begin(), counts.end()); reverse(counts.begin(), counts.end()); int i = 0; int j = counts.size() - 1; int res = 0; while (true) { if (counts[i] == 0) break; counts[i]--; res++; while (counts[i] != 0) { int sub = min(counts[i], counts[j]); counts[i] -= sub; if (i != j) counts[j] -= sub; if (counts[j] == 0) j--; } i++; } printf("%d\n", res); }
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 | #include <algorithm> #include <cstdio> #include <vector> using namespace std; int main () { int n; scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); sort(arr, arr + n); reverse(arr, arr + n); vector<int> counts; counts.push_back(1); for (int i = 1; i < n; i++) { if (arr[i] == arr[i - 1]) counts.back()++; else counts.push_back(1); } sort(counts.begin(), counts.end()); reverse(counts.begin(), counts.end()); int i = 0; int j = counts.size() - 1; int res = 0; while (true) { if (counts[i] == 0) break; counts[i]--; res++; while (counts[i] != 0) { int sub = min(counts[i], counts[j]); counts[i] -= sub; if (i != j) counts[j] -= sub; if (counts[j] == 0) j--; } i++; } printf("%d\n", res); } |