[백준]프린터 큐

jun·2021년 4월 11일
0
post-thumbnail

유의할점

우선 순위 큐

풀이

우선 순위 큐를 활용해서 푼다. 우선순위큐에 있는 순위와 큐에 있는 순위가 동일한지 확인한다.

코드

C++

#include <iostream>
#include <queue>

using namespace std;

int main() {
	int T;
	cin >> T;
	while (T--) {
		int N, M;
		cin >> N >> M;
		priority_queue<int, vector<int>, less<int> > pq;
		queue<int> q;
		int cnt = 0;
			int _;
		for (int i = 0; i < N; i++) {
			cin >> _;
			pq.push(_);
			q.push(_);
		}
		while (!q.empty()) {

			if (pq.top() == q.front()) {
				++cnt;
				pq.pop();
				q.pop();
				if (M == 0) {
					cout << cnt << "\n";
					break;
				}
				else {
					M--;
				}

			}
			else {
				q.push(q.front());
				q.pop();
				if (M == 0)
					M = q.size() - 1;
				else
					M--;
			}
		}
	}
}
profile
Computer Science / Algorithm / Project / TIL

0개의 댓글