Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 13):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#ifndef LOCAL
#define debug(...) 0
#else
#include "/Users/abhij/Desktop/CP/debug.cpp"
#endif

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vpi = vector<pi>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define nl "\n"
#define vt vector
#define pb push_back
#define ins insert
#define eb emplace_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define fbo find_by_order
#define ook order_of_key
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define F first
#define S second
#define cont continue
#define sz(x) (int)x.size()

#define fo(i, a, b) for (int i = (a); i < (b); ++i)
#define fore(i, a, b) for (int i = (a); i <= (b); ++i)
#define rof(i, a, b) for (int i = (a); i > (b); --i)
#define rofe(i, a, b) for (int i = (a); i >= (b); --i)
#define rep(a) fo(_, 0, a)
#define trav(a, x) for (auto &a : x)
#define rv(x) trav(i, x) cin >> i;

const int MOD = 1e9 + 7;

// multi-dim vector
template <int D, typename T> struct mvt : public vector<mvt<D - 1, T>>
{
    static_assert(D >= 1, "Vector dimension must be greater than zero!");
    template <typename... Args> mvt(int n = 0, Args... args) : vector<mvt<D - 1, T>>(n, mvt<D - 1, T>(args...))
    {
    }
};
template <typename T> struct mvt<1, T> : public vector<T>
{
    mvt(int n = 0, const T &val = T()) : vector<T>(n, val)
    {
    }
};

template <class T> using maxpq = priority_queue<T>;
template <class T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <class T> bool ckmin(T &a, const T &b)
{
    return b < a ? a = b, 1 : 0;
}
template <class T> bool ckmax(T &a, const T &b)
{
    return a < b ? a = b, 1 : 0;
}

inline namespace IO
{
void setIO(string name = "")
{
    cin.tie(0)->sync_with_stdio(0);
    if (sz(name))
    {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}
// read
template <class T> void read(T &a)
{
    cin >> a;
}
template <class T, class U> void read(pair<T, U> &p)
{
    read(p.F);
    read(p.S);
}
template <class T> void read(vector<T> &v)
{
    for (T &a : v)
        read(a);
} // dont use with bools
template <class T, class... U> void read(T &a, U &...b)
{
    read(a);
    read(b...);
}
void read(vvi &g, int e)
{
    rep(e)
    {
        int a, b;
        read(a, b);
        g[a].pb(b);
        g[b].pb(a);
    }
}
// print
template <class T> void print(const T &a)
{
    cout << a;
}
void print(const bool b)
{
    cout << (b ? "Yes" : "No");
}
template <class T, class U> void print(const pair<T, U> &p)
{
    print(p.f);
    print(' ');
    print(p.s);
}
template <class T> void print(vector<T> &v)
{ // also don't use with bools
    for (T &a : v)
    {
        print(a);
        print(' ');
    }
}
template <class T, class... U> void print(const T &a, U &...b)
{
    print(a);
    print(' ');
    print(b...);
}
} // namespace IO

struct DSU
{
    vector<int> e;
    DSU(int N)
    {
        e = vector<int>(N, -1);
    }

    // get representive component (uses path compression)
    int get(int x)
    {
        return e[x] < 0 ? x : e[x] = get(e[x]);
    }

    bool same_set(int a, int b)
    {
        return get(a) == get(b);
    }

    int size(int x)
    {
        return -e[get(x)];
    }

    bool unite(int x, int y)
    { // union by size
        x = get(x), y = get(y);
        if (x == y)
            return false;
        if (e[x] > e[y])
            swap(x, y);
        e[x] += e[y];
        e[y] = x;
        return true;
    }
};

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

int pow(int a, int b, int m)
{
    int ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = (ans * a) % m;
        }
        b >>= 1;
        a = (a * a) % m;
    }
    return ans;
}

void solve()
{
    int n, k;
    cin >> n >> k;
    vi a (n);
    read(a);
    if(n == k){
        cout << 1 << nl;
        return;
    }
    unordered_map<int, int> mp;
    for(auto &v : a){
        mp[v]++;
    }
    vi c;
    for(auto[x,y] : mp){
        c.pb(y);
    }
    sort(all(c));
    int i = 0;
    int l = c.size();
    for(; i < l-1; ++i){
        if(k >= c[i]){
            k -= c[i];
        } else{
            break;
        }
    }
    cout << c.size() - i << nl;
}

int main()
{
    setIO();
    int T = 1;
    read(T);
    rep(T) solve();
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#ifndef LOCAL
#define debug(...) 0
#else
#include "/Users/abhij/Desktop/CP/debug.cpp"
#endif

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using vi = vector<int>;
using vb = vector<bool>;
using vpi = vector<pi>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define nl "\n"
#define vt vector
#define pb push_back
#define ins insert
#define eb emplace_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define fbo find_by_order
#define ook order_of_key
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define F first
#define S second
#define cont continue
#define sz(x) (int)x.size()

#define fo(i, a, b) for (int i = (a); i < (b); ++i)
#define fore(i, a, b) for (int i = (a); i <= (b); ++i)
#define rof(i, a, b) for (int i = (a); i > (b); --i)
#define rofe(i, a, b) for (int i = (a); i >= (b); --i)
#define rep(a) fo(_, 0, a)
#define trav(a, x) for (auto &a : x)
#define rv(x) trav(i, x) cin >> i;

const int MOD = 1e9 + 7;

// multi-dim vector
template <int D, typename T> struct mvt : public vector<mvt<D - 1, T>>
{
    static_assert(D >= 1, "Vector dimension must be greater than zero!");
    template <typename... Args> mvt(int n = 0, Args... args) : vector<mvt<D - 1, T>>(n, mvt<D - 1, T>(args...))
    {
    }
};
template <typename T> struct mvt<1, T> : public vector<T>
{
    mvt(int n = 0, const T &val = T()) : vector<T>(n, val)
    {
    }
};

template <class T> using maxpq = priority_queue<T>;
template <class T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <class T> bool ckmin(T &a, const T &b)
{
    return b < a ? a = b, 1 : 0;
}
template <class T> bool ckmax(T &a, const T &b)
{
    return a < b ? a = b, 1 : 0;
}

inline namespace IO
{
void setIO(string name = "")
{
    cin.tie(0)->sync_with_stdio(0);
    if (sz(name))
    {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}
// read
template <class T> void read(T &a)
{
    cin >> a;
}
template <class T, class U> void read(pair<T, U> &p)
{
    read(p.F);
    read(p.S);
}
template <class T> void read(vector<T> &v)
{
    for (T &a : v)
        read(a);
} // dont use with bools
template <class T, class... U> void read(T &a, U &...b)
{
    read(a);
    read(b...);
}
void read(vvi &g, int e)
{
    rep(e)
    {
        int a, b;
        read(a, b);
        g[a].pb(b);
        g[b].pb(a);
    }
}
// print
template <class T> void print(const T &a)
{
    cout << a;
}
void print(const bool b)
{
    cout << (b ? "Yes" : "No");
}
template <class T, class U> void print(const pair<T, U> &p)
{
    print(p.f);
    print(' ');
    print(p.s);
}
template <class T> void print(vector<T> &v)
{ // also don't use with bools
    for (T &a : v)
    {
        print(a);
        print(' ');
    }
}
template <class T, class... U> void print(const T &a, U &...b)
{
    print(a);
    print(' ');
    print(b...);
}
} // namespace IO

struct DSU
{
    vector<int> e;
    DSU(int N)
    {
        e = vector<int>(N, -1);
    }

    // get representive component (uses path compression)
    int get(int x)
    {
        return e[x] < 0 ? x : e[x] = get(e[x]);
    }

    bool same_set(int a, int b)
    {
        return get(a) == get(b);
    }

    int size(int x)
    {
        return -e[get(x)];
    }

    bool unite(int x, int y)
    { // union by size
        x = get(x), y = get(y);
        if (x == y)
            return false;
        if (e[x] > e[y])
            swap(x, y);
        e[x] += e[y];
        e[y] = x;
        return true;
    }
};

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

int pow(int a, int b, int m)
{
    int ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = (ans * a) % m;
        }
        b >>= 1;
        a = (a * a) % m;
    }
    return ans;
}

void solve()
{
    int n, k;
    cin >> n >> k;
    vi a (n);
    read(a);
    unordered_map<int, int, custom_hash> mp;
    for(auto &v : a){
        mp[v]++;
    }
    vi c;
    for(auto[x,y] : mp){
        c.pb(y);
    }
    sort(all(c));
    int l = c.size();
    for(int i = 0; i < l-1; ++i){
        if(k < c[i]){
            cout << l - i << nl;
            return;
        } 
        k -= c[i];
    }
    cout << 1 << nl;
}

int main()
{
    setIO();
    int T = 1;
    read(T);
    rep(T) solve();
}