1966_프린터 큐

bgy·2021년 7월 7일
0

백준

목록 보기
1/21

https://www.acmicpc.net/problem/1966 프린터 큐

s : 답을 저장할 벡터
pair<우선순위, 문서의 처음 위치>
sorted : 우선순위를 오름차순으로 정렬한 벡터

#include <iostream> 
#include<queue>
#include<vector>
#include<utility>
#include<algorithm>
using namespace std;

int main() {
	int n;
	cin >> n;
	int x, y;
	vector<int> s;
	for (int i = 0; i < n; i++) {
		int res = 0;
		cin >> x >> y;
		queue<pair<int,int>> arr;
		vector<int> sorted;
		for (int j = 0; j < x; j++) {
			int m;
			cin >> m;
			arr.push(make_pair(m,j));
			sorted.push_back(m);
		}
		sort(sorted.begin(), sorted.end());
		while (1) {
			if (arr.front().first == sorted.back()) {
				res++;
				if (y == arr.front().second) {
					s.push_back(res);
					break;
				}
				else {
					arr.pop();
					sorted.pop_back();
					continue;
				}
			}
			else {
				arr.push(arr.front());
				arr.pop();
			}
		}
	}
	for (int i = 0; i < s.size(); i++) {
		cout << s[i] << endl;
	}
}

0개의 댓글