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

Previous submission (verdict: Time limit exceeded on test 9):
#include <iostream>
#include <vector>

using namespace std;

// Function to swap two elements
void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

// Function to partition the array and return the pivot index
int partition(vector<int>& arr, int low, int high) {
    // Choose a random pivot
    int pivotIndex = low + (high - low) / 2;
    int pivot = arr[pivotIndex];

    // Swap the pivot with the last element
    swap(arr[pivotIndex], arr[high]);

    int i = low - 1;
    for (int j = low; j < high; j++) {
        if (arr[j] > pivot) {
            i++;
            swap(arr[i], arr[j]);
        }
    }

    // Swap the pivot with the element at the correct position
    swap(arr[i + 1], arr[high]);
    return i + 1;
}

// Function to implement Quicksort
void quicksort(vector<int>& arr, int low, int high) {
    if (low < high) {
        int pivotIndex = partition(arr, low, high);

        // Recursively sort the subarrays
        quicksort(arr, low, pivotIndex - 1);
        quicksort(arr, pivotIndex + 1, high);
    }
}

int main() {

    int t;
    cin >> t;

    for (int i = 0; i < t; i++) {
        int n, k;
        cin >> n >> k;

        if (!n) {
            cout << 0 << endl;
            continue;
        }

        vector<int> nums;
        vector<int> freq;
        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;

            bool x_found = false;

            for (int j = 0; j < nums.size(); j++) {
                if (nums[j] == x) {
                    freq[j]++;
                    x_found = true;
                    break;
                }
            }

            if (!x_found) {
                nums.push_back(x);
                freq.push_back(1);
            }
        }

        quicksort(freq, 0, freq.size() - 1);

        int index = freq.size() - 1;
        while (k) {
            if (freq[index]) {
                freq[index]--;
                freq[0]++;
            } else {
                freq.pop_back();
                index--;
                freq[index]--;
                freq[0]++;
            }

            if (!index) {
                index = 0;
            }

            k--;
        }

        if (!freq[index]) {
            freq.pop_back();
            index--;
        }


        cout << freq.size() << endl;
    }

    return 0;
}


Submission (verdict: Wrong answer on test 1):
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t--) {
        int n, k;
        std::cin >> n >> k;

        std::vector<int> arr(n);
        for (int i = 0; i < n; i++) {
            std::cin >> arr[i];
        }

        std::sort(arr.begin(), arr.end());
        std::vector<int> frequencies = {0};

        for (int i = 0; i < n - 1; i++) {
            if (arr[i] != arr[i+1]) {
                frequencies.emplace_back(1);
            } else {
                frequencies.back()++;
            }
        }

        std::sort(frequencies.begin(), frequencies.end()); // Ascending
        int distinctCount = frequencies.size();

        for (int f : frequencies) {
            if (f <= k) {
                k -= f;
                distinctCount--;
            } else {
                break;
            }
        }

        std::cout << (distinctCount ? distinctCount : 1) << "\n";
    }

}