Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions Seol-JY/202510/26 BOJ G4 카드 섞기.md‎
Original file line number Diff line number Diff line change
@@ -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);
}
}
```
Loading