Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- bool check(vector<int> &count, int N, int mid)
- {
- if (count[N] < mid)
- return false;
- int uni = 0;
- int flag = 0;
- for (int i = 0; i <= N; i++)
- {
- if (count[i])
- {
- uni++;
- }
- }
- if (count[N] == mid)
- {
- uni--;
- }
- return uni >= mid;
- }
- int findOptimalSize(int N, vector<int> &A)
- {
- vector<int> count(N + 1);
- for (int i = 0; i < N; i++)
- {
- count[A[i]]++;
- }
- if (N == 1)
- {
- return 0;
- }
- sort(count.begin(), count.end());
- int start = 1;
- int end = N;
- int ans = 0;
- while (start <= end)
- {
- int mid = (start + end) / 2;
- if (check(count, N, mid))
- {
- ans = mid;
- start = mid + 1;
- }
- else
- {
- end = mid - 1;
- }
- }
- return ans;
- }
- int main()
- {
- int N;
- cin >> N;
- vector<int> A(N);
- for (int i = 0; i < N; i++)
- {
- cin >> A[i];
- }
- cout << findOptimalSize(N, A) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment