Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
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
Archives
Today
Total
관리 메뉴

전공공부

[BOJ_1966] 프린터 큐 본문

Study/Problem Solving

[BOJ_1966] 프린터 큐

monitor 2023. 10. 24. 06:51

설명


자료 구조 섹션은 구현 문제가 많은 것 같다. 인덱스와 크기 값을 저장하고 크기값에 따라서 내림차순 정렬 한 후 그 후 내가 보는 순서가 맞을때 하나씩 빼서 구현한 것 같다.

 

코드


package Data_Structure;

import java.util.*;

public class BOJ_1966 {
    static class Data implements Comparable<Data>{
        int idx;
        int weight;
        public Data (int idx, int weight){
            this.idx = idx;
            this.weight = weight;
        }

        @Override
        public int compareTo(Data o) {
            if(o.weight > this.weight){
                return 1;
            }else if(o.weight < this.weight){
                return -1;
            }else{
                return 0 ;
            }
        }
    }
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int t = 0; t < T; t++) {
            Queue<Data> q = new LinkedList<>();
            List<Data> list = new ArrayList<>();
            int N = sc.nextInt();
            int order = sc.nextInt();

            for(int i = 0; i < N; i++){
                int num = sc.nextInt();
                list.add(new Data(i,num));
                q.offer(new Data(i,num));
            }
            Collections.sort(list);
            int cnt = 0;
            while(true){

                Data num = q.poll();
                if(num.weight == list.get(0).weight){
                    cnt++;
                    list.remove(0);
                    if(num.idx == order){
                        System.out.println(cnt);
                        break;
                    }
                    Collections.sort(list);
                }else{
                    q.offer(num);
                }
            }
        }
    }
}
 

1966번: 프린터 큐

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

www.acmicpc.net

 

'Study > Problem Solving' 카테고리의 다른 글

[BOJ_14425] 문자열 집합  (1) 2023.10.26
[BOJ_1620] 나는야 포켓몬 마스터 이다솜  (0) 2023.10.25
[BOJ_2346] 풍선 터트리기  (1) 2023.10.23
[BOJ_1935] 후위표기식2  (1) 2023.10.22
[BOJ_10866] 덱  (0) 2023.10.21