백준 1966번: 프린터 큐(자바)

Johnny Lee·2026년 4월 14일

백준 1일1제

목록 보기
15/15


백준 클래스 문제를 하나씩 풀며 이번에는 백준 1966번 차례다.
문제를 먼저 보겠다.

링크텍스트


결과:

코드:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner; 

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt(); // 테스트 케이스 수

        for (int i = 0; i < T; i++) {
            int N = sc.nextInt(); // 문서의 개수
            int M = sc.nextInt(); // 궁금한 문서의 위치

            Queue<Document> queue = new LinkedList<>();
            for (int j = 0; j < N; j++) {
                int priority = sc.nextInt();
                queue.add(new Document(j, priority)); // 문서 번호와 중요도 저장
            }

            int printOrder = 0; // 인쇄 순서
            while (!queue.isEmpty()) {
                Document current = queue.poll(); // 큐에서 문서 하나 꺼내기
                boolean hasHigherPriority = false;

                // 현재 문서보다 높은 중요도를 가진 문서가 있는지 확인
                for (Document doc : queue) {
                    if (doc.priority > current.priority) {
                        hasHigherPriority = true;
                        break;
                    }
                }

                if (hasHigherPriority) {
                    queue.add(current); // 현재 문서를 큐의 뒤로 보냄
                } else {
                    printOrder++; // 현재 문서를 인쇄
                    if (current.index == M) { // 궁금한 문서가 인쇄되었는지 확인
                        System.out.println(printOrder);
                        break;
                    }
                }
            }
        }
        sc.close();
    }
    public static class Document {
        int index; // 문서의 원래 위치
        int priority; // 문서의 중요도

        public Document(int index, int priority) {
            this.index = index;
            this.priority = priority;
        }
    }
    
}
profile
You can always be better

0개의 댓글