File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed
Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class boj1091 {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static StringTokenizer st;
8+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
9+ static int nextInt() {return Integer . parseInt(st. nextToken());}
10+
11+ static int N ;
12+ static int [] P , S , cur, start;
13+ public static void main(String [] args) throws Exception {
14+ int answer = 0 ;
15+ nextLine();
16+ N = nextInt();
17+ nextLine();
18+ P = new int [N ];
19+ cur = new int [N ];
20+ start = new int [N ];
21+ for (int i = 0 ; i < N ; i++ ) {
22+ P [i] = nextInt();
23+ cur[i] = i;
24+ }
25+ start = cur. clone();
26+ nextLine();
27+ S = new int [N ];
28+ for (int i = 0 ; i < N ; i++ ) S [i] = nextInt();
29+
30+ if (check()) {
31+ System . out. println(0 );
32+ return ;
33+ }
34+
35+ while (true ) {
36+ int [] temp = new int [N ];
37+ for (int i = 0 ; i < N ; i++ ) temp[S [i]] = cur[i];
38+ answer++ ;
39+ cur = temp;
40+ if (check()) break ;
41+ if (Arrays . equals(cur, start)) {
42+ answer = - 1 ;
43+ break ;
44+ }
45+ }
46+ System . out. println(answer);
47+ }
48+
49+ static boolean check() {
50+ int [] pos = new int [N ];
51+ for (int i = 0 ; i < N ; i++ ) pos[cur[i]] = i;
52+ for (int i = 0 ; i < N ; i++ ) {
53+ if (pos[i] % 3 != P [i]) return false ;
54+ }
55+ return true ;
56+ }
57+ }
58+ ```
You can’t perform that action at this time.
0 commit comments