입력:
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1
출력:
1
2
5
백준 1966번 "프린터 큐" 문제의 코드입니다.
//1966
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int _testCase,n,_imp,cnt,k,v;
cin>>_testCase;
for(int t=0;t<_testCase;t++){
cnt=0;
cin>>n>>_imp;
queue<pair<int,int>> q;
priority_queue<int> pq; //우선순위 큐.
for(int i=0;i<n;i++){
int tmp;
cin>>tmp;
pq.push(tmp);
q.push({i,tmp});
}
while(!q.empty()){
tie(k,v)=q.front();//first,second
q.pop();
if(pq.top()==v){
pq.pop();
cnt++;
if(k==_imp){
cout<<cnt<<"\n";
break;
}
}
else q.push({k,v});
}
}
return 0;
}
위의 코드를 예제의 입력을 넣어 실행했을 때의 결과입니다.
백준 10828 스택 C++ (0) | 2023.04.11 |
---|---|
백준 10845 큐 C++ (0) | 2023.04.11 |
백준 2583 영역 구하기 C++ (0) | 2023.04.10 |
백준 2468 안전 영역 C++ (0) | 2023.04.10 |
백준 1181 단어 정렬 C++ (0) | 2023.04.07 |