Little Jay

[C++] 백준 1966번 프린터 큐 본문

알고리즘/BOJ

[C++] 백준 1966번 프린터 큐

Jay, Lee 2021. 7. 16. 19:49

처음에 큐라고만 써있어서 생으로 QUEUE로만 구현하려고 하다가

이건 좀 아닌거 같은데라는 생각이 들어

우선순위 큐로 구현하였다

 

#include <iostream>
#include <queue>
using namespace std;

int main() {

	int testCase, x;
	cin >> testCase;
	int document, which;
	for (int i = 0; i < testCase; i++) {
		cin >> document >> which;
		int count = 0;
		queue<pair<int, int>> q;
		priority_queue <int> pq;
		for (int k = 0; k < document; k++) {
			cin >> x;
			q.push({ k, x });
			pq.push(x);
		}
		while (!q.empty()) {
			int index = q.front().first;
			int value = q.front().second;
			q.pop();
			if (pq.top() == value) {
				pq.pop();
				count++;
				if (index == which) {
					cout << count << "\n";
					break;
				}
			}
			else
				q.push({ index, value });
		}
	}
	return 0;
}
Comments