CF1882E2.Two Permutations (Hard Version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The difference between the two versions is that you have to minimize the number of operations in this version. You can make hacks only if both versions of the problem are solved.
You have two permutations † p1,p2,…,pn (of integers 1 to n ) and q1,q2,…,qm (of integers 1 to m ). Initially pi=ai for i=1,2,…,n , and qj=bj for j=1,2,…,m . You can apply the following operation on the permutations several (possibly, zero) times.
In one operation, p and q will change according to the following three steps:
- You choose integers i , j which satisfy 1≤i≤n and 1≤j≤m .
- Permutation p is partitioned into three parts using pi as a pivot: the left part is formed by elements p1,p2,…,pi−1 (this part may be empty), the middle part is the single element pi , and the right part is pi+1,pi+2,…,pn (this part may be empty). To proceed, swap the left and the right parts of this partition. Formally, after this step, p will become pi+1,pi+2,…,pn,pi,p1,p2,…,pi−1 . The elements of the newly formed p will be reindexed starting from 1 .
- Perform the same transformation on q with index j . Formally, after this step, q will become qj+1,qj+2,…,qm,qj,q1,q2,…,qj−1 . The elements of the newly formed q will be reindexed starting from 1 .
Your goal is to simultaneously make pi=i for i=1,2,…,n , and qj=j for j=1,2,…,m .
Find any way to achieve the goal using the minimum number of operations possible, or say that none exists. Please note that you have to minimize the number of operations.
† A permutation of length k is an array consisting of k distinct integers from 1 to k in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation ( 2 appears twice in the array), and [1,3,4] is also not a permutation ( k=3 but there is 4 in the array).
输入格式
The first line contains two integers n and m ( 1≤n,m≤2500 ).
The second line contains n integers a1,a2,…,an ( 1≤ai≤n ).
The third line contains m integers b1,b2,…,bm ( 1≤bi≤m ).
It is guaranteed that a and b are permutations.
输出格式
If there is no solution, print a single integer −1 .
Otherwise, print an integer k — the number of operations to perform, followed by k lines, each containing two integers i and j ( 1≤i≤n , 1≤j≤m ) — the integers chosen for the operation.
If there are multiple solutions, print any of them.
Please note that you have to minimize the number of operations.
输入输出样例
输入#1
3 5 2 1 3 5 2 1 4 3
输出#1
2 3 4 2 4
输入#2
4 4 3 4 2 1 2 4 1 3
输出#2
3 3 3 1 4 4 2
输入#3
2 2 1 2 2 1
输出#3
-1
说明/提示
In the first test case, we can achieve the goal within 2 operations:
- In the first operation, choose i=3 , j=4 . After this, p becomes [3,2,1] and q becomes [3,4,5,2,1] .
- In the second operation, choose i=2 , j=4 . After this, p becomes [1,2,3] and q becomes [1,2,3,4,5] .
In the third test case, it is impossible to achieve the goal.