BOJ
[백준][c++] 1966번: 프린터 큐
LEE MINGYU
2024. 10. 2. 15:50
https://www.acmicpc.net/problem/1966
문제 해석
- 큐를 이용한 우선순위 구하기
알고리즘 분류
- 자료구조
- 큐
풀이
- 순서(num)와 우선순위(prioty)를 가진 class를 큐에 저장하고
- 순회하며 제일 우선순위가 큰지 검사
코드
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
int n, m, t;
class Num {
public:
int num;
int prioty;
Num(int a, int b) : num(a), prioty(b){};
};
void solution() {
cin >> n >> m;
queue<Num> q;
for (int i=0; i<n; i++) {
int prioty;
cin >> prioty;
q.push(Num(i, prioty));
}
int count = 0;
while (!q.empty()) {
bool flag = true; //우선순위가 가장 큰가?
Num current = q.front();
queue<Num> tempQ = q;
while (!tempQ.empty()) {
Num temp = tempQ.front();
tempQ.pop();
if(temp.prioty > current.prioty) {
flag = false;
break;
}
}
q.pop();
if (flag) {
count++;
if(current.num == m) {
cout << count << "\n";
return;
}
}
else
q.push(current);
}
}
int main() {
cin >> t;
for(int i = 0; i<t; i++) solution();
return 0;
}