There are $N$ pupils in a primary school class with class number $1$ to $N$.
The pupil with class number $A[i]$ sits on the $i$-th seat for $1 \leq i \leq N$.
Rock Lee, the class teacher, would like to change the pupils' seats in the beginning of each month, so that they can make more friends with their neighbouring pupils.
This time, he wants to have the $i$-th seat sat by the pupil with class number $B[i]$.
He also wants to make the changing process as fast as possible.
However, the pupils do not know how to shift one another's seats. They can only exchange seats with another pupil in 1 minute.
Therefore, Rock is going to ask some pairs of pupils to exchange seats with each other for each minute.
Note that Rock can tell multiple pairs of pupils to exchange seats simutaneously. However, he cannot ask a pupil to exchange seats with multiple pupils as this would confuse them.
Unfortunately, Rock has forgot all of the algorithms he learnt in his Computer Science lectures during his university study.
You are going to help Rock for the fastest way of changing seats.
Input
The first line contains an integer $N$.
The second line contain $N$ integers. The $i$-th one is $A[i]$.
The second line contain $N$ integers. The $i$-th one is $B[i]$.
Output
On the first line, output a single integer $T$, the fastest time in minutes.The following lines describe the actions in the $T$ minutes.
In the $i$-th minute ($1 \leq i \leq T$):
- The first line contains an integer $m_i$, the number of actions.
-
The following $m_i$ lines contains 2 integers $x_{ij}$, $y_{ij}$ each ($x_{ij} \neq y_{ij}$), meaning the pupils of class number $x_{ij}$ and $y_{ij}$ exchange seats with each other.
Note that $\{ x_{ip}, y_{ip} \} ∩ \{ x_{iq}, y_{iq} \} = ∅$ for $ 1 \leq p, q \leq m_i$.
Constraints
For all subtasks:- $2\leq N \leq 2 × 10^5$
- $A,B$ are permutations of $[1..N]$
Subtask 1 (3%): $N \leq 5$
Subtask 2 (21%): for $1 \leq i \leq N$, $B[i]=A[1+(i+K-1)$ $mod$ $N]$ for some constant $K$.
Subtask 3 (24%): $N \leq 3000$
Subtask 4 (52%): No additional constraints.
Sample Test Cases
Input | Output | |
---|---|---|
5 2 3 5 1 4 3 4 1 5 2 |
2 2 3 4 5 1 1 2 3 |
|
1st minute: 2 3 5 1 4
-> 2 3 5 1 4
-> 2 4 1 5 3 2nd minute: 2 4 5 1 3 -> 2 4 5 1 3 -> 3 4 1 5 2 |
Scoring: Per Subtask
Authored by s19l08
Appeared in 2023 Mini Contest 9 (Pre-HKOI)