[BOJ] 1966번 프린터 큐 - C++

"큐, 덱"

Posted by Yongmin on June 21, 2021

문제

프린터 큐

풀이

중요도의 내림차순을 나타내는 imp 어레이와 큐 자료구조로 넣은 기존의 testcase, 그리고, 해당 문서인지 아닌지의 여부를 확인해주는 check 어레이를 선언해주었다. 중요도 순서에 해당되나, 해당 문서가 아니라면(=false) pop을 해주어 printf를 해주면서 cnt를 +1 해주고, 중요도 순서에도 포함 안되고, 해당 문서도 아니라면 앞에 있는 원소를 뒤로 보내준다. 그러다가 해당 문서이며(=true) 또 중요도 순서가 찾아 왔다면 cnt를 +1 해주고 while문을 마치며, cnt를 출력하며 정답을 도출해낼 수 있다.

소스 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;

int desc(int a, int b){
    return a > b;
}
int main(){
    int N;
    scanf("%d", &N);
        
    
    for(int i = 0; i<N; i++){
        int input1, input2;
        scanf("%d %d", &input1, &input2);
        
        queue<int> arr;
        vector<int> imp;
        for(int j = 0; j<input1; j++){
            int temp;
            scanf("%d", &temp);
            arr.push(temp);
            imp.push_back(temp);
        }
        sort(imp.begin(), imp.end(), desc);
        
        queue<bool> check;
        for(int i = 0; i<input1; i++){
            if(i == input2){
                check.push(true);
            }
            else check.push(false);
        }
       
        
        
       
        
        int cnt = 0;
        int num = 0;
        while(1){
            if(check.front() == true && imp[num] == arr.front()){
                cnt++;
                break;
            }
            
            if(check.front() == false && imp[num] == arr.front()){
                arr.pop();
                check.pop();
                num++;
                cnt++;
            }
            else{
                int temp = arr.front();
                arr.pop();
                arr.push(temp);
                
                bool bool_temp = check.front();
                check.pop();
                check.push(bool_temp);
            }
            
            
            
        }
        
        printf("%d\n", cnt);

         }

}


# # #