Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cstring>
#include <math.h>
#include <ostream>
#include <string>
#include <type_traits>
#include <vector>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>


using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define LOCAL
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (_VA_ARGS_) << "] "


#define arr array
#define ll long long
#define ld long double
#define sz(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
#define read(v) for (auto &u : v) cin >> u;
#define print(v) for (auto u : v) cout << u << ' ';




vector<pair<ll,ll>> nb_premier(ll x){

        vector<pair<ll,ll>>res;
        if(!(x&1)){
                ll cnt=0;
        while(!(x&1)){
            x/=2;cnt++;
        }
        res.pb({2,cnt});
        }
        for(ll i=3;i*i<=x;i+=2){
            if(! (x%i)) {
                ll c=0;
                while(!(x%i)){
                x/=i;c++;}
            res.pb({i,c});
            }
        }



        if(x>1) res.pb({x,1});
        return res;
}
ll power(ll n,ll p)
{
    ll res=1;
    for(ll i=0;i<p;i++)
    {
        res*=n;

    }
    return res;
}
vector<bool>seive(int n){

    vector<bool>prime(n+1,true);

    for(int i=2;i*i<=n;++i){
        if(prime[i]){

            for(int j=i*i;j<=n;j+=i){
                prime[j]=false;
            }
        }
    }
    return prime;

}



// you should actually read the stuff at the bottom
const ll M=1e9+7;
const int MAX_N = 100005;

int dfs(int node, vector<vector<int>>& adj,vector<int>& stars, vector<bool>& visited) {
    stack<int> s;
    vector<int> results;
    s.push(node);
    visited[node] = true;
    int res=0;
    while (!s.empty()) {
        int current = s.top();
        s.pop();
        int star=stars[current];

        bool test=false,test1=false;
        for (int neighbor : adj[current]) {
            if (!visited[neighbor]) {
                test=true;
                if(stars[neighbor] > stars[current]){
                ++res;test1=true;
                visited[neighbor] = true;
                s.push(neighbor);
                }
                else {
                    test1=false;
                visited[neighbor] = true;
                s.push(neighbor);
                }
            }
    }
    if(!test) {
        results.push_back(res);
        if(test1)--res;
    }

}
return *max_element(results.begin(),results.end());

}
int gcd(int a, int b) {

    if (b == 0) {
        return a;
    }

    return gcd(b, a % b);
}


pair<ll, ll> find_max_distance_pair(ll l, ll r, ll G) {

    ll a_min=(l+G-1)/G;
    ll a_max=r/G;

    ll best_A = -1, best_B = -1;
    ll max_distance = -1;
    for (ll a = a_min; a <= a_max; ++a) {
        ll A = G * a;
        for (ll b = a_max; b >= a; --b) {
            ll B = G * b;
            if (gcd(a, b) == 1) {
                int distance = abs(A - B);
                if (distance > max_distance) {
                    max_distance = distance;
                    best_A = A;
                    best_B = B;
                }
                break;
            }
        }
    }

    return make_pair(best_A, best_B);
}


void test_case()
{
   ll n,k;cin>>n>>k;
   vector<ll>a(n);for(ll i=0;i<n;++i) cin>>a[i];
   ll x=*max_element(a.begin(),a.end())+1;
   map<ll,ll>cnt;
   for(ll e:a)
   {
       cnt[e]++;
   }

   ll z=0;
    ll res=0;
   for(auto e:cnt)
   {
       if(z+e.second<=k)
       {
           z+=e.second;
           e.second=0;
       }else
       {
           ++res;

       }
   }
if(res==0) cout<<"1\n";
else cout<<res<<endl;



}







int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(15);
    int t=1;
    cin>>t;
    while(t--)
        test_case();

return 0;

}


Submission (verdict: Accepted):
#include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cstring>
#include <math.h>
#include <ostream>
#include <string>
#include <type_traits>
#include <vector>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>


using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define LOCAL
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (_VA_ARGS_) << "] "


#define arr array
#define ll long long
#define ld long double
#define sz(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl '\n'
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
#define read(v) for (auto &u : v) cin >> u;
#define print(v) for (auto u : v) cout << u << ' ';




vector<pair<ll,ll>> nb_premier(ll x){

        vector<pair<ll,ll>>res;
        if(!(x&1)){
                ll cnt=0;
        while(!(x&1)){
            x/=2;cnt++;
        }
        res.pb({2,cnt});
        }
        for(ll i=3;i*i<=x;i+=2){
            if(! (x%i)) {
                ll c=0;
                while(!(x%i)){
                x/=i;c++;}
            res.pb({i,c});
            }
        }



        if(x>1) res.pb({x,1});
        return res;
}
ll power(ll n,ll p)
{
    ll res=1;
    for(ll i=0;i<p;i++)
    {
        res*=n;

    }
    return res;
}
vector<bool>seive(int n){

    vector<bool>prime(n+1,true);

    for(int i=2;i*i<=n;++i){
        if(prime[i]){

            for(int j=i*i;j<=n;j+=i){
                prime[j]=false;
            }
        }
    }
    return prime;

}



// you should actually read the stuff at the bottom
const ll M=1e9+7;
const int MAX_N = 100005;

int dfs(int node, vector<vector<int>>& adj,vector<int>& stars, vector<bool>& visited) {
    stack<int> s;
    vector<int> results;
    s.push(node);
    visited[node] = true;
    int res=0;
    while (!s.empty()) {
        int current = s.top();
        s.pop();
        int star=stars[current];

        bool test=false,test1=false;
        for (int neighbor : adj[current]) {
            if (!visited[neighbor]) {
                test=true;
                if(stars[neighbor] > stars[current]){
                ++res;test1=true;
                visited[neighbor] = true;
                s.push(neighbor);
                }
                else {
                    test1=false;
                visited[neighbor] = true;
                s.push(neighbor);
                }
            }
    }
    if(!test) {
        results.push_back(res);
        if(test1)--res;
    }

}
return *max_element(results.begin(),results.end());

}
int gcd(int a, int b) {

    if (b == 0) {
        return a;
    }

    return gcd(b, a % b);
}


pair<ll, ll> find_max_distance_pair(ll l, ll r, ll G) {

    ll a_min=(l+G-1)/G;
    ll a_max=r/G;

    ll best_A = -1, best_B = -1;
    ll max_distance = -1;
    for (ll a = a_min; a <= a_max; ++a) {
        ll A = G * a;
        for (ll b = a_max; b >= a; --b) {
            ll B = G * b;
            if (gcd(a, b) == 1) {
                int distance = abs(A - B);
                if (distance > max_distance) {
                    max_distance = distance;
                    best_A = A;
                    best_B = B;
                }
                break;
            }
        }
    }

    return make_pair(best_A, best_B);
}


void test_case()
{
   ll n,k;cin>>n>>k;
   vector<ll>a(n);for(ll i=0;i<n;++i) cin>>a[i];
   sort(a.begin(),a.end());
   vector<ll>cnt={1};
   for(ll i=1;i<n;++i)
   {
       if(a[i]==a[i-1])
       {
           cnt.back()++;
       }else
       {
           cnt.pb(1);
       }
   }
   ll m=cnt.size();
   sort(cnt.begin(),cnt.end());
   ll x=0;
   for(ll i=0;i<m;++i)
   {
       if(x+cnt[i]<=k)
       {
           x+=cnt[i];
       }
       else{
        cout<<m-i<<endl;return;
       }
   }
   cout<<"1\n";



}







int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(15);
    int t=1;
    cin>>t;
    while(t--)
        test_case();

return 0;

}