728x90
큐 문제입니다.
우선순위가 있는 수열에서 큐에 넣었을때 m번째 수는
몇번째에 출력되는지 찾아야 합니다.
값들을 priority_queue에 넣고
일반 queue에는 값과 인덱스 번호를 넣습니다.
우선순위 큐의 가장 위에 값과 같을 때 까지 큐를 계속 돌립니다.
같아지면 우선순위 큐의 가장위를 pop하고 다시 반복합니다.
최종적으로 찾는 인덱스가 나오면 count를 출력합니다.
count는 우선순위 큐를 pop할 때마다 세어주면 됩니다.
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int main(int argc, char *argv[])
{
int ts;
int n, m;
int c;
int num;
int here;
scanf("%d", &ts);
while (ts--) {
c = 0;
queue<pair<int, int>> q;
priority_queue<int> pq;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", &num);
q.push({ num ,i });
pq.push(num);
}
while (q.size()) {
here = q.front().first;
num = q.front().second;
q.pop();
if (pq.top() == here) {
++c;
pq.pop();
if (num == m) {
break;
}
}
else q.push({ here,num });
}
printf("%d\n", c);
}
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 1021번 회전하는 큐 (0) | 2019.09.08 |
---|---|
[C++] 백준 10866번 덱 (0) | 2019.09.08 |
[C++] 백준 11866번 조세퍼스 문제 0 (0) | 2019.09.08 |
[C++] 백준 10845번 큐 (0) | 2019.09.07 |
[C++] 백준 17298번 오큰수 (2) | 2019.09.05 |