알고리즘

C++ 알고리즘 - 백준 1966 프린터 큐

마루설아 2025. 1. 1. 15:13

https://www.acmicpc.net/problem/1966

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

int main(void) {
	// C++ Init
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int input1, input2, input3, input4;
	int seq, chk;

	cin >> input1;

	for (int i = 0; i < input1; i++) {
		deque<int> dq;
		priority_queue<int> pqu;
		seq = 1;

		cin >> input2 >> input3;
		chk = input3;
		
		for (int j = 0; j < input2; j++) {
			cin >> input4;
			dq.push_back(input4);
			pqu.push(input4);
		}

		if (input2 == 1) {
			cout << seq << endl;
			continue;
		}

		while (true) {
			if (dq.front() != pqu.top()) {
				int num = dq.front();
				dq.pop_front();
				dq.push_back(num);

				chk--;
				if (chk < 0) chk = dq.size() - 1;

				continue;
			}

			else if (dq.front() == pqu.top()) {
				if (chk == 0) {
					cout << seq << endl;
					break;
				}

				else {
					dq.pop_front();
					pqu.pop();
					seq++;

					chk--;
					if (chk < 0) chk = dq.size() - 1;
				}
			}
		}
	}
}