[백준] 1966번: 프린터 큐

ByWindow·2021년 9월 6일
0

Algorithm

목록 보기
58/104
post-thumbnail

📝문제

문제에서 버젓이 라고 명시되어 있어서 Queue를 사용해서 푸는가? 생각했지만
큐를 사용했을 때 해당 인덱스의 인쇄물의 중요도와 이후의 인쇄물들의 중요도를 어떻게 비교해야하는지 방법이 떠오르지 않았다.
그래서 ArrayList를 사용했고, Queue의 poll() 대신 get()과 remove() 메서드를 사용했다.

📌코드

package Baekjoon;

import java.util.*;
import java.io.*;

public class BOJ1966 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        StringTokenizer st;
        for(int i = 0; i < t; i++){
            st = new StringTokenizer(br.readLine());
            int n = Integer.parseInt(st.nextToken());//문서 개수
            int m = Integer.parseInt(st.nextToken());
            st = new StringTokenizer(br.readLine());
            ArrayList<int[]> list = new ArrayList<>();
            for(int j = 0; j < n; j++){
                list.add(new int[] {j, Integer.parseInt(st.nextToken())});
            }
            int cnt = 0;
            while(!list.isEmpty()){
                int[] cur = list.get(0);
                boolean moveLast = false;
                for(int j = 1; j < list.size(); j++){
                    if(cur[1] < list.get(j)[1]) {
                        moveLast = true;
                        break;
                    }
                }
                if(moveLast){
                    list.add(cur);
                    list.remove(0);
                }else{
                    if(cur[0] == m){
                        cnt++;
                        System.out.println(cnt);
                        break;
                    }else{
                        list.remove(0);
                        cnt++;
                    }
                }
            }
        }

    }
}
profile
step by step...my devlog

0개의 댓글