카테고리 없음

백준 1966 - 프린터 큐 ( C++ )

jin_j_i_n 2022. 2. 23. 23:39

https://www.acmicpc.net/problem/1966

 

1966번: 프린터 큐

여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에

www.acmicpc.net

 

1. 우선순위를 배열에 저장 후 내림차순으로 정렬

2. pair<Int, Int> 저장하는 큐 생성 후 우선순위 높은대로 pop 시키기, count + 1 

3. 목표 인덱스의 문서를 찾았다면 count 출력 후 종료

 

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[]) {
    
    int N = 0;
    cin >> N;
    
    while(N--) {
        int n, i, size, count = 0;
        cin >> n >> i;
        size = n;
        int priority[n] ;
        
        queue<pair<int, int>> que; //인덱스, 우선순위 값
        
        while(size--) {
            int a;
            cin >> a;
            que.push(make_pair(n - size - 1, a));
            
            priority[size] = a;
        }
        
        sort(priority, priority + n);
        
        int idx = n - 1 ;
        
        while(idx >= 0 ) {
            if(priority[idx] != que.front().second) {
                pair<int, int> tmp = que.front();
                que.pop();
                que.push(tmp);
            }else{
                pair<int, int> tmp = que.front();
                count++;
                idx--;
                que.pop();
                if (tmp.first == i) {
                    cout << count << endl;
                    break;
                }
            }
        }
    }
    
    return 0;
}