#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> c(n + 1, 0);
for (int i = 0; i < n; i++) {
int a;
cin >> a;
c[a]++;
}
// for (auto &v : c) cout << v << ", ";
// cout << "dupa\n";
sort(c.begin(), c.end());
// for (auto &v : c) cout << v << ", ";
// cout << "dupa\n";
int i = 1;
int j = n;
int ans = 0;
while (i <= j) {
// cout << "iter\n";
while (i <= j && c[j] == 0) --j;
int cnt = c[j];
if (c[j] == 0) break;
// cout << j << endl;
while (i < j && c[j] > 1) {
if (c[i] == 0) {
++i;
continue;
}
c[i]--;
c[j]--;
}
++ans;
--j;
}
cout << ans << endl;
}
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> c(n + 1, 0); for (int i = 0; i < n; i++) { int a; cin >> a; c[a]++; } // for (auto &v : c) cout << v << ", "; // cout << "dupa\n"; sort(c.begin(), c.end()); // for (auto &v : c) cout << v << ", "; // cout << "dupa\n"; int i = 1; int j = n; int ans = 0; while (i <= j) { // cout << "iter\n"; while (i <= j && c[j] == 0) --j; int cnt = c[j]; if (c[j] == 0) break; // cout << j << endl; while (i < j && c[j] > 1) { if (c[i] == 0) { ++i; continue; } c[i]--; c[j]--; } ++ans; --j; } cout << ans << endl; } |
English