BOJ_S3_1966
๐ [S3_1966] ํ๋ฆฐํฐ ํ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
	static StringTokenizer st;
	static Queue<int[]> queue;
	static int[] temp;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		// ํ
์คํธ ์ผ์ด์ค ์
๋ ฅ
		int tc = Integer.parseInt(br.readLine());
		int z = 0;
		for (int t = 1; t <= tc; t++) {
			st = new StringTokenizer(br.readLine(), " ");
			// ๋ฌธ์์ ๊ฐ์
			int N = Integer.parseInt(st.nextToken());
			// ๊ถ๊ธํ ๋ฌธ์๊ฐ ๋ช๋ฒ์งธ ๋์ฌ์๋์ง
			int M = Integer.parseInt(st.nextToken());
			st = new StringTokenizer(br.readLine(), " ");
			// ์ค์๋ ๋ฐฐ์ด ์
๋ ฅ
			LinkedList<int[]> q = new LinkedList<int[]>();
			for (int i = 0; i < N; i++) {
				int input = Integer.parseInt(st.nextToken());
				q.add(new int[] { i, input }); // (index, ์ค์๋)
			}
			// ๋ช๋ฒ์งธ๋ก ์ธ์
			int res = 0;
			while (!q.isEmpty()) {
				// ์ฐ์ ์์ ํ์ธ ๋ณ์
				boolean isMax = true;
				// ์ ค ์ ์์ ์ถ์ถ
				int[] p = q.poll();
				for (int i = 0; i < q.size(); i++) {
					if (p[1] < q.get(i)[1]) {
						q.add(p);
						for (int j = 0; j < i; j++) {
							q.add(q.poll());
						}
						isMax = false;
						break;
					}
				}
				if (isMax == false) {
					continue;
				}
				res++;
				if (p[0] == M) {
					break;
				}
			}
			sb.append(res).append("\n");
		}
		System.out.println(sb);
	}
}
๐ค ๋์ ์๊ฐ
๋ฌธ์ ์๋ ๋์์์ง๋ง Queue๋ฅผ ์ด์ฉํด์ ํธ๋ ๋ฌธ์ ์ด๋ค.
ํ๋ค๋ณด๋ LinkedList๊ฐ ๋ชจ๋  ์์๋ฅผ ๋น๊ตํด ์ค ์ ์์ด์ LinkedList๋ก ๋ฐ๊ฟจ๋ค.
์ธ ์ ์๋ ๋ฉ์๋๋ ๋ ๋ง๊ณ  ์ด์ฐจํผ Queue๋ ์์ํ๊ณ  ์๊ธฐ ๋๋ฌธ์ ํ๊ฐ ์ฐ๋ ๋ฉ์๋๋ ๋ค ์จ์ ๊ตฌํํ์๋ค. ( ๋ฌผ๋ก  Queue๋ ์์จ๋ ๋๋ค )
์ฌ๊ธฐ์๋ LinkedList๋ฅผ ์ฐ๊ธฐ ์ ์ ๋ฌธ์ ๋ ๋ญ๊ฐ ๋ฐ๋ก ์ ๋๋ก ์ดํดํ์ง ๋ชปํ๊ณ  ์ฐ์ ์์๋ฅผ ์ ํด์ฃผ๋ ๊ณณ์์ ๋ง์ด ๋ฒ๋ฒ
์๋ค..
๊ทธ๋์ ์๊ฐ์ด ์ข ๋  .. ์์ฌ์ด ๋ฌธ์ ๋ค.