문제 제목 : 프린터 큐

난이도 : 하

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

추천 풀이 시간 : 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()));
                }
            }
        }
    }
}

'Algorithm > BAEKJOON' 카테고리의 다른 글

[알고리즘-DP] 파도반 수열  (0) 2023.01.20
[알고리즘-DP] 2×n 타일링  (0) 2023.01.20
[자료구조-스택] 스택 수열  (2) 2023.01.20
[자료구조-배열] 블랙잭  (0) 2023.01.19
[자료구조-배열] 음계  (0) 2023.01.19

+ Recent posts