알고리즘/백준

1966번 : 프린터 큐

코딍코딍 2022. 7. 16. 18:07

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

 

1966번: 프린터 큐

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

www.acmicpc.net

 

 

문제

 

 

코드

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt(); sc.nextLine();
        StringBuilder sb = new StringBuilder();

        for(int i=0;i<t;i++) {
            int n = sc.nextInt(); int m = sc.nextInt();
            sc.nextLine();
            
            int max=0;
            int srr[] = new int[n];
            
            for(int j=0;j<n;j++) {
                srr[j] = sc.nextInt();
                if(srr[j]>max) max=srr[j];
            }
            
            int count=1;
            int index=0;
            while(true) {
                if(srr[index]>=max){
                    if(index==m) break;
                    srr[index]=-1;
                    max = Arrays.stream(srr).max().getAsInt();
                    count++;
                }
                index++;
                index%=n;
            }
            
            sb.append(count+"\n");
        }
        System.out.println(sb);
    }
}

 

 

해결방법

슈도코드

  1. 중요도 입력받으면서 max값 구하기
  2. 무한 loop 시작
    1. 중요도가 max보다 크거나 같은 경우
      1. index가 몇 번째로 인쇄되었는지 궁금한 문서와 같은 위치면 탈출
      2. 같지 않으면 count 증가, 해당 중요도 값 -1로 설정, max 값 재설정
  3. StringBuilder에 count 추가

위의 방법을 테스트 케이스 수 만큼 반복하면 된다.

 

Queue를 써서 풀 수도 있을 것 같았지만 복잡해 질 것 같아서 일반 배열에 넣고 푸는 방법으로 풀었다.

Queue를 써서도 풀어봐야겠다.