#P6718. Recovering the Flip Operations
Recovering the Flip Operations
Recovering the Flip Operations
Given a permutation of length \(N\) denoted by \(a_i\), we define a flip operation on an interval \([l,r]\) as follows:
Find the minimum and maximum values in \(a[l\ldots r]\) (i.e. \(\min\{a_l,a_{l+1},\ldots,a_r\}\) and \(\max\{a_l,a_{l+1},\ldots,a_r\}\)), and then swap their positions.
A sequence of flip operations was applied to an initial permutation to produce a final permutation. In this problem, you are given \(N\), the initial permutation, and the final permutation. Under the guarantee that the final permutation is obtained by applying either zero or one flip operation on the initial permutation, your task is to recover the flip operation that was performed.
Note: If no operation is needed (i.e. the initial permutation equals the final one), output 0. If exactly one valid flip operation recovers the final permutation from the initial permutation, output 1 in the first line and then output the interval \(l\) and \(r\) (1-indexed) in the next line. Otherwise, if no valid single flip operation exists, output -1.
inputFormat
The first line contains an integer \(N\) (e.g. \(1 \le N \le 10^5\)).
The second line contains \(N\) space-separated integers representing the initial permutation.
The third line contains \(N\) space-separated integers representing the final permutation.
outputFormat
If no flip operation is needed, output a single integer 0
.
If exactly one flip operation is needed, first output an integer 1
on a line, then output two space-separated integers \(l\) and \(r\) (1-indexed) on the next line.
If it is impossible to recover a valid flip operation under the given guarantee, output -1
.
sample
5
1 3 2 4 5
1 3 2 4 5
0