Assumption is All You Need

면접 대비

시간 제한1초메모리 제한1024 MB

요약
두 순열 A와 B가 주어질 때, A의 역전 쌍을 교환하는 연산만으로 A를 B로 바꾸는 교환 순서를 구하거나 불가능함을 판별한다.
난이도

보통10점 중 7점

유형
그리디, 정렬, 배열, 구현
정답자
아직 제출이 없습니다

문제

JB holds the belief that assumption is all you need to solve a problem. In order to prove that, JB has given you two permutations of numbers from 11 to nn: AA and BB, and JB wants you to output a sequence of element swapping operation (x_i,y_i)(x\_i,y\_i) on AA, so that:

  1. every pair of swapped element forms an inversion pair (i.e. x_i<y_ix\_i < y\_i and A_x_i>A_y_iA\_{x\_i} > A\_{y\_i} when the ii-th operation is being performed)
  2. AA will become BB at the end of the swapping sequence.

or determine it is impossible. Help prove JB's belief by solving this problem!

입력

There are multiple test cases. The first line of the input contains one integer TT indicating the number of test cases. For each test case:

The first line contains one integer nn (1≤n≤2,0211 \le n \le 2\\,021), indicating the number elements in AA and BB.

The second line contains nn distinct integers A_1,A_2,…,A_nA\_1,A\_2,\dots,A\_n (1≤A_i≤n1 \le A\_i \le n), indicating the array AA.

The third line contains nn distinct integers B_1,B_2,…,B_nB\_1,B\_2,\dots,B\_n (1≤B_i≤n1 \le B\_i \le n), indicating the array BB.

It is guaranteed that the sum of nn in all test cases will not exceed 2,0212\\,021.

출력

For each test case, if there doesn't exist a sequence, output the one line containing one integer "-1".

Otherwise, in the first line output one integer kk (0≤k≤n(n−1)20 \le k \le \frac{n(n-1)}{2}), indicating the length of the swapping sequence. Then, output kk line each containing two integers x_ix\_i and y_iy\_i (1≤x_i<y_i≤n1 \le x\_i < y\_i \le n), indicating the ii-th operation swap(A_x_i,A_y_i)\text{swap}(A\_{x\_i},A\_{y\_i}).

예제1

  1. 예제 1

    입력
    3
    2
    1 2
    2 1
    4
    4 1 2 3
    1 3 2 4
    8
    8 7 6 5 4 3 2 1
    1 8 7 6 5 4 3 2
    
    예상 출력
    -1
    2
    1 2
    2 4
    7
    7 8
    6 7
    5 6
    4 5
    3 4
    2 3
    1 2