Algorithm/BAEKJOON

[자료구조-큐] 프린터 큐

imsseong 2023. 1. 20. 09:32

문제 제목 : 프린터 큐

난이도 : 하

문제 유형 : 큐, 구현, 그리디

추천 풀이 시간 : 25분 (못하면 2배 50분)

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

 

1966번: 프린터 큐

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

www.acmicpc.net

 

import java.util.*;

class Docs {
    private int index;
    private int priority;
    
    public Docs(int index, int priority) {
        this.index = index;
        this.priority = priority;
    }
    
    public int getIndex() {
        return this.index;
    }
    
    public int getPriority() {
        return this.priority;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        int n;
        int m;
       
        for (int i = 0; i < tc; i++) {
            n = sc.nextInt();
            m = sc.nextInt();
            
            Queue<Docs> docsQueue = new LinkedList<Docs>();
            Queue<Integer> prioQueue = new PriorityQueue<Integer>();
            
            for (int j = 0; j < n; j++) {
                int priority = sc.nextInt();
                docsQueue.add(new Docs(j, priority));
                prioQueue.add(-priority);
            }
            
            int cnt = 0;
            while (true) {
                Docs docs = docsQueue.poll();
                if (docs.getPriority() >= -prioQueue.peek()) {
                    cnt++;
                    prioQueue.poll();
                
                    if (docs.getIndex() == m) {
                        System.out.println(cnt);
                        break;
                    }
                } else {
                    docsQueue.add(new Docs(docs.getIndex(), docs.getPriority()));
                }
            }
        }
    }
}