728x90
320x100
1966번: 프린터 큐
여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에
www.acmicpc.net
반응형
📌 접근 방법
- q<pair<int,int>>를 이용하여 몇번째로 출력했는지 궁금한 출력물은 1, 아닌 출력물들은 0으로 구분해서 우선순위와 함께 넣어둔다.
- 벡터에도 따로 우선순위를 넣어둔다
- 우선순위대로 출력하게 하고, 카운팅한다
- 우선순위가 가장 큰 순서대로 출력하고 아니라면 맨 뒤로 넘긴다
- 몇번째로 출력했는지 궁금한 출력물이 출력될 때 반복문을 탈출해서 해당 카운트를 출력한다.
✅ Pass Code
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iterator>
#include<map>
#include<set>
#include<unordered_set>
#include <stack>
#include<queue>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin>>T;
int N,M,cnt=0;
for(int i=0; i<T; i++) {
queue<pair<int, int>> q;
vector<int> v;
cin >> N >> M;
cnt=0;
for (int j = 0; j < N; j++) {
int x;
cin >> x;
v.emplace_back(x);
if (j == M) {
q.push(pair<int, int>(x, 1));
} else {
q.push(pair<int, int>(x, 0));
}
}
sort(v.begin(), v.end());
while (true) {
if(v[v.size()-1]==q.front().first){
cnt++;
if(q.front().second==1){
q.pop();
v.erase(v.end()-1);
break;
}
q.pop();
v.erase(v.end()-1);
}
else{
q.push(q.front());
q.pop();
}
}
cout<<cnt<<'\n';
}
return 0;
}
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/003.gif)
728x90
반응형
'Coding Test > Baekjoon' 카테고리의 다른 글
[BOJ/백준/C++] 5430번 AC (1) | 2024.01.28 |
---|---|
[BOJ/백준/C++] 1021번 회전하는 큐 (0) | 2024.01.28 |
[BOJ/백준/C++] 11866번 요세푸스 문제 0 (1) | 2024.01.28 |
[BOJ/백준/C++] 1874번 (0) | 2024.01.28 |
[BOJ/백준/C++] 20920번 영단어 암기는 괴로워 (0) | 2024.01.28 |