Chef in Vaccination Queue codechef solution in c++

include

include

using namespace std;

int main() { int t; cin >> t;

while (t--) {
    int n, m;
    cin >> n >> m;

    queue<int> q;
    priority_queue<int> pq;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        q.push(x);
        pq.push(x);
    }

    int count = 0;
    while (!q.empty()) {
        int front = q.front();
        q.pop();

        if (front == pq.top()) {
            count++;
            pq.pop();
            if (m == 0) {
                break;
            }
        } else {
            q.push(front);
        }

        m = (m - 1 + n) % n;
    }

    cout << count << endl;
}

return 0;

}

Explanation: 1. The code starts by taking the number of test cases, t, as input from the user. 2. The code then enters a while loop that runs t times, representing each test case. 3. Inside the while loop, the code takes two integers, n and m, as input. n represents the number of people in the vaccination queue, and m represents the position of the chef in the queue. 4. The code initializes two queues: q (a normal queue) and pq (a priority queue). 5. The code then takes n integers as input and adds them to both q and pq. This represents the heights of the people in the queue. 6. The code initializes a count variable to keep track of the number of people the chef can vaccinate before his turn. 7. The code enters a while loop that runs until the q queue is empty. 8. Inside the while loop, the code retrieves the front element of the q queue using the front() function and stores it in the front variable. 9. If the front element is equal to the top element of the pq priority queue (which stores the maximum height), it means that the chef can vaccinate this person. 10. In this case, the count variable is incremented by 1, the top element of the pq priority queue is removed using the pop() function, and m is checked if it is 0. If m is 0, it means that it is the chef's turn to be vaccinated, so the loop is broken using the break statement. 11. If the front element is not equal to the top element of the pq priority queue, it means that the chef cannot vaccinate this person yet. In this case, the front element is added back to the q queue using the push() function. 12. Finally, the m variable is updated by subtracting 1 and taking modulo n, which represents the new position of the chef in the queue. 13. After the while loop ends, the count variable is printed, representing the number of people the chef could vaccinate before his turn for each test case.