Logo
(追記) (追記ここまで)

20629번 - Permutation Sort 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB53221837.500%

문제

One day (call it day 0), you find a permutation $P$ of $N$ integers written on the blackboard in a single row. Fortunately you have another permutation $Q$ of $N$ integers, so you decide to play with these permutations.

Every morning of the day 1, 2, 3 you rewrite every number on the blackboard in such a way that erases the number $x$ and write the number $Q_x$ at the same position. Please find the minimum non-negative integer $d$ such that in the evening of the day $d$ the sequence on the blackboard is sorted in increasing order.

입력

The input consists of a single test case in the format below.

The first line of the input contains an integer $N$ (1ドル \le N \le 200$). The second line contains $N$ integers $P_1,ドル $\ldots,ドル $P_N$ (1ドル \le P_i \le N$) which represent the permutation $P$. The third line contains $N$ integers $Q_1,ドル$\ldots,ドル$Q_N$ (1ドル \le Q_i \le N$ which represent the permutation $Q$.

출력

Print the minimum non-negative integer $d$ such that in the evening of the day $d$ the sequence on the blackboard is sorted in increasing order. If such $d$ does not exist, print $-1$ instead. It is guaranteed that the answer does not exceed 10ドル^{18}$.

제한

예제 입력 1

6
2 3 1 4 5 6
3 1 2 5 4 6

예제 출력 1

4

예제 입력 2

6
1 2 3 4 5 6
3 1 2 5 4 6

예제 출력 2

0

예제 입력 3

6
2 3 1 4 5 6
3 4 5 6 1 2

예제 출력 3

-1

힌트

출처

Camp > Petrozavodsk Programming Camp > Summer 2020 > Day 5: JAG Summer+ Opening Contest D번

Contest > ICPC Japanese Alumni Group > JAG Summer Camp > JAG Summer Camp 2019 Day 1 D번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /