본문으로 바로가기

[BaekJoon] 백준 1966 프린터 큐

category 알고리즘/백준 2020. 10. 15. 19:19

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

 

1966번: 프린터 큐

첫 줄에 test case의 수가 주어진다. 각 test case에 대해서 문서의 수 N(100이하)와 몇 번째로 인쇄되었는지 궁금한 문서가 현재 Queue의 어떤 위치에 있는지를 알려주는 M(0이상 N미만)이 주어진다. 다음

www.acmicpc.net

 

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

public class Main {
	static class Doc implements Comparable<Doc>{
		int priority;
		int number;
		public Doc(int p, int n) {
			this.priority = p; this.number = n;
		}
		
		@Override
		public int compareTo(Doc d) {
			if(this.priority > d.priority)
				return 1;
			else 
				return -1;
		}
		
	}
	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			int test_case = Integer.parseInt(br.readLine());
			for(int i = 0; i<test_case; i++) {
				String[] str = br.readLine().split(" ");
				int N = Integer.parseInt(str[0]);
				int M = Integer.parseInt(str[1]);
				
				ArrayList<Doc> printer = new ArrayList<>();
				String[] priority = br.readLine().split(" ");
				int target = 0;
				
				for(int j = 0; j<N; j++) {
					printer.add(new Doc(Integer.parseInt(priority[j]), j));
				}
				int count = 0;
				boolean flag = false;
				while(printer.size() > 1) {
					Doc d = printer.remove(0);
					Doc max = Collections.max(printer);
					
					if(d.priority < max.priority) {
						printer.add(d);
					}
					else {		//print
						count++;
						if(d.number == M) {
							flag = true; break;
						}
							
					}
				}
				
				if(!flag)
					count++;
				
				System.out.println(count);
			}
		}catch(IOException e) {	
		}
	}
	
}

 

 

list를 사용해서 풀었다. collection.max를 사용해서 가장 큰 값을 뽑을 수 있다.