From 4cc435e3559939d62041bb6fc32a4478e5d67e33 Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Sun, 26 Oct 2025 23:51:44 +0900 Subject: [PATCH] =?UTF-8?q?[20251026]=20BOJ=20/=20G4=20/=20=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=20=EC=84=9E=EA=B8=B0=20/=20=EC=84=A4=EC=A7=84?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \354\204\236\352\270\260.md\342\200\216" | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 "Seol-JY/202510/26 BOJ G4 \354\271\264\353\223\234 \354\204\236\352\270\260.md\342\200\216" diff --git "a/Seol-JY/202510/26 BOJ G4 \354\271\264\353\223\234 \354\204\236\352\270\260.md\342\200\216" "b/Seol-JY/202510/26 BOJ G4 \354\271\264\353\223\234 \354\204\236\352\270\260.md\342\200\216" new file mode 100644 index 00000000..62b6ed6d --- /dev/null +++ "b/Seol-JY/202510/26 BOJ G4 \354\271\264\353\223\234 \354\204\236\352\270\260.md\342\200\216" @@ -0,0 +1,82 @@ +```java + +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)); + + int N = Integer.parseInt(br.readLine()); + + int[] P = new int[N]; + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + P[i] = Integer.parseInt(st.nextToken()); + } + + int[] S = new int[N]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + S[i] = Integer.parseInt(st.nextToken()); + } + + int[] current = new int[N]; + for (int i = 0; i < N; i++) { + current[i] = i; + } + + boolean isGoal = true; + for (int i = 0; i < N; i++) { + if (current[i] % 3 != P[i]) { + isGoal = false; + break; + } + } + + if (isGoal) { + System.out.println(0); + return; + } + + int maxAttempts = 500000; + + for (int shuffle = 1; shuffle <= maxAttempts; shuffle++) { + int[] next = new int[N]; + for (int i = 0; i < N; i++) { + next[i] = current[S[i]]; + } + current = next; + + boolean success = true; + for (int i = 0; i < N; i++) { + if (current[i] % 3 != P[i]) { + success = false; + break; + } + } + + if (success) { + System.out.println(shuffle); + return; + } + + if (shuffle > 1) { + boolean backToStart = true; + for (int i = 0; i < N; i++) { + if (current[i] != i) { + backToStart = false; + break; + } + } + if (backToStart) { + System.out.println(-1); + return; + } + } + } + + System.out.println(-1); + } +} +```