B - 整列の部分修正 解説 /

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

高橋君のクラスには N 人の生徒がおり、生徒には 1 から N までの出席番号がついています。

現在、生徒たちは一列に並んでおり、左から順に出席番号 p_1, p_2, \ldots, p_N の生徒が並んでいます。先生が指定した目標の並び順は、左から順に出席番号 q_1, q_2, \ldots, q_N です。

高橋君は、列の中から連続する区間を 1 つ選び、その区間内の生徒の並び順を反転させる操作をちょうど 1行います。具体的には、1 \leq L \leq R \leq N を満たす整数 L, R を選び、位置 L から位置 R までの生徒の順番を逆にします。L = R の場合は並びは変化しませんが、これも 1 回の操作を行ったものとみなします。

L, R の選び方を適切に決めることで、操作後の並びを目標の並び q と一致させることができるか判定してください。

制約

  • 1 \leq N \leq 10^6
  • (p_1, p_2, \ldots, p_N)(1, 2, \ldots, N) の順列である
  • (q_1, q_2, \ldots, q_N)(1, 2, \ldots, N) の順列である
  • 入力はすべて整数である

入力

N
p_1 p_2 \cdots p_N
q_1 q_2 \cdots q_N
  • 1 行目には、生徒の人数を表す整数 N が与えられる。
  • 2 行目には、現在の並び順を表す順列 p の要素 p_1, p_2, \ldots, p_N がスペース区切りで与えられる。
  • 3 行目には、目標の並び順を表す順列 q の要素 q_1, q_2, \ldots, q_N がスペース区切りで与えられる。

出力

ちょうど 1 回の反転操作で現在の並びを目標の並びに一致させることができるなら Yes を、できないなら No を出力してください。


入力例 1

5
1 2 3 4 5
1 4 3 2 5

出力例 1

Yes

入力例 2

4
1 2 3 4
2 1 4 3

出力例 2

No

入力例 3

10
3 1 4 2 5 6 7 8 9 10
3 1 8 7 6 5 2 4 9 10

出力例 3

Yes

入力例 4

25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
1 2 3 4 5 6 20 19 18 17 16 15 14 13 12 11 10 9 8 7 21 22 23 24 25

出力例 4

Yes

入力例 5

1
1
1

出力例 5

Yes

Score : 300 pts

Problem Statement

There are N students in Takahashi's class, and the students are assigned attendance numbers from 1 to N.

Currently, the students are standing in a line, and from left to right, the students with attendance numbers p_1, p_2, \ldots, p_N are lined up. The target order specified by the teacher is, from left to right, attendance numbers q_1, q_2, \ldots, q_N.

Takahashi will select one contiguous interval from the line and reverse the order of students within that interval exactly once. Specifically, he chooses integers L, R satisfying 1 \leq L \leq R \leq N, and reverses the order of students from position L to position R. When L = R, the arrangement does not change, but this is still considered as having performed one operation.

Determine whether it is possible to make the arrangement after the operation match the target arrangement q by appropriately choosing L and R.

Constraints

  • 1 \leq N \leq 10^6
  • (p_1, p_2, \ldots, p_N) is a permutation of (1, 2, \ldots, N)
  • (q_1, q_2, \ldots, q_N) is a permutation of (1, 2, \ldots, N)
  • All inputs are integers

Input

N
p_1 p_2 \cdots p_N
q_1 q_2 \cdots q_N
  • The first line contains an integer N representing the number of students.
  • The second line contains the elements p_1, p_2, \ldots, p_N of the permutation p representing the current order, separated by spaces.
  • The third line contains the elements q_1, q_2, \ldots, q_N of the permutation q representing the target order, separated by spaces.

Output

If it is possible to make the current arrangement match the target arrangement with exactly one reversal operation, print Yes; otherwise, print No.


Sample Input 1

5
1 2 3 4 5
1 4 3 2 5

Sample Output 1

Yes

Sample Input 2

4
1 2 3 4
2 1 4 3

Sample Output 2

No

Sample Input 3

10
3 1 4 2 5 6 7 8 9 10
3 1 8 7 6 5 2 4 9 10

Sample Output 3

Yes

Sample Input 4

25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
1 2 3 4 5 6 20 19 18 17 16 15 14 13 12 11 10 9 8 7 21 22 23 24 25

Sample Output 4

Yes

Sample Input 5

1
1
1

Sample Output 5

Yes