#include <iostream>
#include <queue>

using namespace std;

int main() {
    int N, M;
    
    // 1. 환자 수와 궁금한 환자 번호를 입력받음
    cin >> N >> M;

    queue<pair<int, int>> q; // {위험도, 환자번호}

    // 2. 초기 대기열 만들기
    for (int i = 0; i < N; i++) {
        int danger;
        cin >> danger;
        q.push({danger, i});
    }

    int count = 0; // 진료 받은 순서

    while (!q.empty()) {
        // 맨 앞의 환자를 일단 꺼냄
        int cur_danger = q.front().first;
        int cur_idx = q.front().second;
        q.pop();

        bool has_more_danger = false;
        int s = q.size();

        // 3. 현재 환자보다 위험한 사람이 있는지 큐를 한 바퀴 돌며 확인
        for (int i = 0; i < s; i++) {
            if (q.front().first > cur_danger) {
                has_more_danger = true;
            }
            // 확인한 환자는 순서가 바뀌지 않게 다시 뒤로 보냄
            q.push(q.front());
            q.pop();
        }

        if (has_more_danger) {
            // 뒤에 더 위험한 사람이 있다면 다시 맨 뒤로 줄 서기
            q.push({cur_danger, cur_idx});
        } else {
            // 내가 제일 위험하므로 진료 받음
            count++;
            
            // 내가 궁금해하던 환자(M)라면 결과 출력 후 종료
            if (cur_idx == M) {
                cout << count << endl;
                break;
            }
        }
    }

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: