#include <iostream> #include <algorithm> using namespace std; constexpr int maxN = 500'007; constexpr int maxVal = 500'007; int n; int z[maxVal]; int ileciagow = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; z[x]++; } // z+n sorts till 0->n-1, and z+n+1 till 0->n; // and values are up till n sort(z, z+n+1); int coveredUp = 0; int rest = n; for (int i = n; i >= 0; i--) { if (z[i] == 0) { break; } coveredUp += z[i]-1; ileciagow++; rest -= z[i]; if (coveredUp >= rest) { // all the rest can be assigned to already created ciagi break; } } cout << ileciagow << '\n'; 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 47 48 49 50 | #include <iostream> #include <algorithm> using namespace std; constexpr int maxN = 500'007; constexpr int maxVal = 500'007; int n; int z[maxVal]; int ileciagow = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; z[x]++; } // z+n sorts till 0->n-1, and z+n+1 till 0->n; // and values are up till n sort(z, z+n+1); int coveredUp = 0; int rest = n; for (int i = n; i >= 0; i--) { if (z[i] == 0) { break; } coveredUp += z[i]-1; ileciagow++; rest -= z[i]; if (coveredUp >= rest) { // all the rest can be assigned to already created ciagi break; } } cout << ileciagow << '\n'; return 0; } |