Advertisement
Guest User

hello 2025 B

a guest
Jan 4th, 2025
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. int f(vector<int> &a)
  9. {
  10.     set<int> s;
  11.     for(int &i : a)
  12.     {
  13.         s.insert(i);
  14.     }
  15.     return s.size();
  16. }
  17.  
  18. bool comp(pair<int,int> p1, pair<int,int> p2)
  19. {
  20.     return p2.second > p1.second;
  21. }
  22.  
  23. void solve()
  24. {
  25.     int n,k;
  26.     cin >> n >> k;
  27.     vector<int> a(n);
  28.     // here I first used unordered map which gave tle
  29.     map<int,int> freq;
  30.     for(int i = 0; i < n; i++)
  31.     {
  32.         cin >> a[i];
  33.         freq[a[i]]++;
  34.     }
  35.     vector<pair<int,int>> fq;
  36.     for(auto &x : freq)
  37.     {
  38.         fq.push_back({x.first,x.second});
  39.     }
  40.     sort(fq.begin(),fq.end(),comp);
  41.     int ct = 0;
  42.     for(auto &x : fq)
  43.     {
  44.         if(k >= x.second)
  45.         {
  46.             k -= x.second;
  47.             ct++;
  48.         }
  49.     }
  50.     cout << max(f(a) - ct,1) << endl;
  51. }
  52.  
  53. int main()
  54. {
  55.     ios_base::sync_with_stdio(false);
  56.     cin.tie(NULL);
  57.     ll t;
  58.     cin >> t;
  59.     while(t--)
  60.     {
  61.         solve();
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement