[ 다먹살 ]/- Coding

[백준] 1966 프린터 큐

엉망으로살기 2021. 11. 15. 11:25
반응형

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

 

1966번: 프린터 큐

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

www.acmicpc.net

 

아마 Queue를 이용해도 충분히 해결가능 했을 것 같다. 일단 나는 중요도에 대한 정렬이 필요하다고 생각했기 때문에 Collections.sort를 이용할 수 있는 ArrayList를 이용했다. 그 외의 부분은 큐를 이용한 방식과 상당히 비슷하게 해결했다.


문제 및 입출력

 


코드

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        int t = sc.nextInt();
        
        for(int i=0; i<t; i++)
        {
            int n = sc.nextInt();
            int index = sc.nextInt();
            int cnt = 0;
            
            ArrayList<int[]> docs = new ArrayList<int[]>();
            ArrayList<Integer> priority = new ArrayList<Integer>();
            
            for(int j=0; j<n; j++)
            {
                int temp = sc.nextInt();
                docs.add(new int[]{j, temp}); // 문서의 인덱스와 중요도를 함께 저장
                priority.add(temp); // 문서 중요도만 ArrayList에 저장
            }
            
            // case 1 : 문서 개수가 1일때는 무조건 결과값으로 1출력
            if(n==1)
            {
                sb.append(1 + "\n");
            }
            // case 2 : 문서 개수가 1이 아닐때는 아래 step에서 처리
            else
            {
                // case 2-1 : 문서 중요도를 내림차순으로 정렬
                Collections.sort(priority, Collections.reverseOrder());

                while(true)
                {
                    // case 2-2 : 문서 대기열 맨 앞의 중요도와 체크할 중요도가 같을 때
                    if(priority.get(0)==docs.get(0)[1])
                    {
                        cnt++;
                        
                        // case 2-2-1 : 중요도도 같고, 문서 인덱스도 같을 때(종료 조건)
                        if(index==docs.get(0)[0])
                        {
                            sb.append(cnt + "\n");
                            break;
                        }
                        // case 2-2-1 : 중요도는 같지만, 문서 인덱스가 다르면 대기열에서 제거
                        else
                        {
                            docs.remove(0);
                            priority.remove(0);
                        }
                    }
                    // case 2-3 : a
                    else
                    {
                        docs.add(docs.remove(0));
                    }
                }
            }
        }
        
        System.out.println(sb.toString());
        sc.close();
    }
}

 

반응형