분류 : 자료구조
https://www.acmicpc.net/problem/1966
여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에 쌓여서 FIFO - First In First Out - 에 따라 인쇄가 되게 된다. 하지만 상근이는 새로운 프린터기 내부 소프트웨어를 개발하였는데, 이 프린터기는 다음과 같은 조건에 따라 인쇄를 하게 된다.
예를 들어 Queue에 4개의 문서(A B C D)가 있고, 중요도가 2 1 4 3 라면 C를 인쇄하고, 다음으로 D를 인쇄하고 A, B를 인쇄하게 된다.
여러분이 할 일은, 현재 Queue에 있는 문서의 수와 중요도가 주어졌을 때, 어떤 한 문서가 몇 번째로 인쇄되는지 알아내는 것이다. 예를 들어 위의 예에서 C문서는 1번째로, A문서는 3번째로 인쇄되게 된다.
1<=N<=100, 0<=M< N
ex)
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1
-> 3개의 테스트케이스
1번 테스트 케이스 : 문서 1개, 0번째 문서
2번 테스트 케이스 : 문서 4개, 2번째 문서
3번 테스트 케이스 : 문서 6개, 0번째 문서
중요한 점은 자신보다 높은 중요도 문서가 존재하면 큐의 '맨 뒤'로 재배치된다는 점이다.
초기 출력 횟수 - 0
*linked list는 add() 대신 offer() 사용
import com.sun.org.apache.xpath.internal.operations.String;
import java.util.LinkedList;
import java.util.Scanner;
public class printerQueue {
public static void solution(){
Scanner in = new Scanner(System.in);
//정답
StringBuilder answer = new StringBuilder();
//테스트케이스 개수
int tc = in.nextInt();
//각 테스트 케이스 당 한번씩
for(int i = 0; i<tc ; i++){
int N = in.nextInt(); //문서 개수
int M = in.nextInt(); //문서 위치
LinkedList<int[]> queue = new LinkedList<>();
//System.out.println(N+" "+M);
//앞선 프린트 횟수
int count = 0;
for(int j = 0; j<N;j++){
queue.offer(new int[] {j,in.nextInt()}); //문서 위치, 문서 중요도 순으로 큐에 저장
}
while(!queue.isEmpty()){
//현재 맨 앞 문서
int[] current = queue.poll();
boolean maxPriority = true;
for(int l=0;l<queue.size();l++){
//더 큰 원소 존재 -> 뒤로 보내줌
if(current[1]<queue.get(l)[1]){
queue.offer(current);
maxPriority = false;
break;
}
}
//더 큰 원소가 없었으면
if(maxPriority){
count++;
//System.out.println("count increase "+count);
//찾으려던 문서이면
if(current[0]==M){
//정답에 붙이기
//System.out.println("test " + count);
answer.append(count).append('\n');
break;
}
}
else{
//System.out.println("no increase");
}
}
}
System.out.println(answer);
}
}