Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 109 ms):
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " ";  _print(x); cerr << endl;
#else
#define debug(x)
#endif
// #define ll long long int
#define int long long int
#define vint vector<int>
#define vlong vector<ll>
#define mod1 1000000007
#define mod2  998244353
#define pi 3.141592653589793238462643
#define loop(i,a,b) 	for(int i=(a);i<=(b);i++)
#define looprev(i,a,b) 	for(int i=(a);i>=(b);i--)
#define all(x)  x.begin(), x.end()

#define print(n) cout << (n) << endl
#define pb push_back
#define ff first
#define ss second
#define pii pair<int,int> 
#define pll pair<ll,ll>
#define mp make_pair
#define pyes cout << "YES" << endl
#define pno cout << "NO" << endl
#define vpii vector<pii>
#define vpll vector<pll>
#define vstr vector<string>
#define getSet(x) __builtin_popcountll(x)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); 
#define sz(v) (int) v.size()
#define iint(n) int n; cin >> n;
#define ivec1(v,n) vint v; loop(_,0,n-1) {iint(___); v.pb(___);}
#define input1(v,n) loop(_,0,n-1) {iint(___); v.pb(___);}
#define ivec2(v,n) vlong v; loop(_,0,n-1) {ill(___); v.pb(___);}
#define input2(v,n) loop(_,0,n-1) {ill(___); v.pb(___);}
#define prvec(arr,n) loop(_,0,n-1){ cout << arr[_] << " ";} cout << endl;
#define vsort(vc) sort(all(vc))
#define vrsort(vc) sort(vc.rbegin(),vc.rend())
void _print(bool t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(deque <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(deque <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
//set in decreasing order
/*
auto cmp = [](pii const &x, pii const &y) {
    return x > y;
};
set<pii, decltype(cmp)> v(cmp);
bool cmp(pair<string,int> p1,pair<string,int> p2){
    if (p1.ss != p2.ss)
        return p1.ss > p2.ss;
    return p1.ff < p2.ff;
}
remove all consecutive duplicacy
v.erase(unique(all(v)),v.end());
count unique;
distance(v.begin(),unique(all(v)));
*/
//-------------------------------------------------------------------------------------------------

bool cmp(pii &a,pii &b){
    if (a.ff != b.ff) return a.ff > b.ff;
    else return a.ss < b.ss;
}

void solve(int t){
    int n,k; cin >> n >> k;
    vint v; input1(v,n);

    map<int,int> m;
    for (auto x : v){
        m[x]++;
    }

    vpii arr;
    for (auto x : m){
        arr.pb(mp(x.ss,x.ff));
    }
    sort(all(arr),cmp);
    int ans = 1;
    looprev(i,sz(arr)-1,1){

        if (arr[i].ff <= k){
            k-=arr[i].ff;
            arr[i].ff = 0;
        }
        else{
            ans = i+1;
            break;
        }
    }

    print(ans);

}

//-------------------------------------------------------------------------------------------------
signed main()
{
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("Error.txt","w",stderr);
    auto start_time = chrono::steady_clock::now();
#endif   
    int t; t = 1; 
    // preComp();
    cin >> t;
    loop(i,1,t)
        solve(i);
    
#ifndef ONLINE_JUDGE
    auto end_time = chrono::steady_clock::now();
    auto diff_time = end_time - start_time;
    cout << "Execution time : " << fixed << setprecision(5) <<  chrono::duration <double, milli> (diff_time).count() << " ms" << endl;
#endif
    return 0;
}

Solution 2 (runtime: 233 ms):
#include <bits/stdc++.h>
using namespace std;

int mostFrequent(map <int, int> &freq) {
    int ans = 0;
    for (auto it: freq) {
        ans = max(ans, it.second);
    }
    
    return ans;
}

int main() {
	// your code goes here
    int tc;
    cin >> tc;
    while(tc--) {
        int n, k;
        cin >> n >> k;
        vector <int> nums(n);
        map <int, int> freq;
        for (int i = 0; i < n; i++) {
            cin >> nums[i];
            freq[nums[i]]++;
        }
        
        if (n == 1) {
            cout << 1 << endl;
            continue;
        }
        if (k == 0) {
            cout << freq.size() << endl;
            continue;
        }
        
        int most = mostFrequent(freq);
        
        priority_queue <int, vector <int>, greater <int>> pq;
        int cnt = 0;
        for (auto it: freq) {
            if (it.second < most) {
                pq.push(it.second);
            }
            else {
                if (cnt == 0) {
                    cnt += 1;
                    continue;
                }
                else pq.push(it.second);
            }
        }
        
        while (!pq.empty()) {
            if (pq.top() > k) {
                int val = pq.top();
                pq.pop();
                pq.push(val - k);
                k = 0;
                break;
            }
            else {
                k -= pq.top();
                pq.pop();
            }
        }
        
        int ans = 1;
        while (!pq.empty()) {
            ans += 1;
            pq.pop();
        }
        
        cout << ans << endl;
        
    }
}