Algorithm/📖Baekjoon

#1966 프린터 큐

yewoneeee 2022. 6. 28. 15:00

# 문제

# 입력 및 출력

# 풀이

우선순위 큐 하나와 그냥 큐 하나를 선언해서 풀었음

우선순위 큐를 사용해선 중요도가 가장 높은 인쇄물을 찾고

그냥 큐를 사용해서 현재 인쇄 대기중인 대기열을 확인하도록 했음

 

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

int main() {
	int T, N, lo, num;
	cin >> T;
	while (T--) {
		priority_queue<int, vector<int>> f;
		queue<pair<int, int>> q;
		int cnt = 1;
		cin >> N >> lo;
		for (int i = 0; i < N; i++) {
			cin >> num;
			f.push(num);
			q.push(make_pair(i, num));
		}
		while (1) {
			if (f.top() == q.front().second) { // 가장 높은 우선순위를 가진 문서가 출력할 순서가 됨
				if (q.front().first == lo) break; // 원하는 문서가 출력할 순서가 되면 종료
				q.pop();
				f.pop();
				cnt++;
			}
			else { // 가장 높은 우선순위를 큐의 가장 앞에 두기 위해 push, pop 반복
				q.push(q.front());
				q.pop();
			}
		}
		cout << cnt << "\n";
	}
}