본문 바로가기
Programming/Algorithm

백준 큐 1966

by OKOK 2018. 1. 29.

#include <cstdio>

#include <algorithm>

#include <queue>

using namespace std;

int t, n, m, x, r;

int main() {

    scanf("%d", &t);

    while (t--) {

        r = 0;

        queue<pair<int, int>> qu;

        priority_queue<int> pq;

        scanf("%d%d", &n, &m);

        for (int i = 0; i < n; i++) {

            scanf("%d", &x);

            qu.push({ x,i });

            pq.push(x);

        }

        while (qu.size()) {

            int here = qu.front().first;

            int num = qu.front().second;

            qu.pop();

            if (pq.top() == here) {

                r++;

                pq.pop();

                if (num == m)break;

            }

            else qu.push({ here,num });

        }

        printf("%d\n", r);

    }

    return 0;

}

 


우선순위 큐와 큐의 사용법을 익히는 것인데, 문제가 대략적으로 이해되고, 구현도 대략적으로만 이해가 됩니다. 큐의 중요성을 아직 인지하지 못했습니다.