Samkit5025

Untitled

Jun 27th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool check(vector<int> &count, int N, int mid)
  5. {
  6.     if (count[N] < mid)
  7.         return false;
  8.  
  9.     int uni = 0;
  10.     int flag = 0;
  11.     for (int i = 0; i <= N; i++)
  12.     {
  13.         if (count[i])
  14.         {
  15.             uni++;
  16.         }
  17.     }
  18.  
  19.     if (count[N] == mid)
  20.     {
  21.         uni--;
  22.     }
  23.  
  24.     return uni >= mid;
  25. }
  26.  
  27. int findOptimalSize(int N, vector<int> &A)
  28. {
  29.  
  30.     vector<int> count(N + 1);
  31.     for (int i = 0; i < N; i++)
  32.     {
  33.         count[A[i]]++;
  34.     }
  35.     if (N == 1)
  36.     {
  37.         return 0;
  38.     }
  39.     sort(count.begin(), count.end());
  40.  
  41.     int start = 1;
  42.     int end = N;
  43.     int ans = 0;
  44.     while (start <= end)
  45.     {
  46.         int mid = (start + end) / 2;
  47.         if (check(count, N, mid))
  48.         {
  49.             ans = mid;
  50.             start = mid + 1;
  51.         }
  52.         else
  53.         {
  54.             end = mid - 1;
  55.         }
  56.     }
  57.  
  58.     return ans;
  59. }
  60.  
  61. int main()
  62. {
  63.  
  64.     int N;
  65.     cin >> N;
  66.  
  67.     vector<int> A(N);
  68.     for (int i = 0; i < N; i++)
  69.     {
  70.         cin >> A[i];
  71.     }
  72.  
  73.     cout << findOptimalSize(N, A) << endl;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment