백준 1966번(Java)

박은지·2025년 2월 24일

백준

목록 보기
36/89
post-thumbnail

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

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());
		
		while(T-- > 0) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			
			int N = Integer.parseInt(st.nextToken());
			int M = Integer.parseInt(st.nextToken());
			
			LinkedList<int[]> queue = new LinkedList<>();
			st = new StringTokenizer(br.readLine());
			
			for(int i=0; i<N; i++) {
				// {위치, 중요도}
				queue.offer(new int[] {i, Integer.parseInt(st.nextToken())});
			}
			
			int count = 0;
			
			while(!queue.isEmpty()) {
				int[] front = queue.poll(); // 첫원소
				boolean isMax = true; // front가 가장 큰 원소인가?
				
				for(int i=0; i<queue.size(); i++) {
					
					// 처음 뽑은 값보다 큐에 있는 원소 중요도가 더 크면
					if(front[1] < queue.get(i)[1]) {
						// 뽑은 원소 & i이전의 값을 뒤로 보냄
						queue.offer(front);
						for(int j=0; j<i; j++) {
							queue.offer(queue.poll());
						}
						isMax = false; // front 원소가 가장 큰게 아니므로 탐색종료
						break;
					}
				}
				
				if(isMax == false) {
					continue;
				}
				
				count++; // front 원소가 가장 큰 값이였으니 출력해야하는 문서
				if(front[0] == M) { // 찾아야하는 값이면 종료
					break;
				}
			}
			
			sb.append(count).append('\n');
		}
		System.out.println(sb);
	}
}
profile
백엔드 개발자가 되고싶은 eunzi😊

0개의 댓글