QOJ.ac

QOJ

時間限制: 1.5 s 記憶體限制: 512 MB 總分: 100 可 Hack ✓

#6838. 假设就是你所需要的一切

统计

JB 坚信“假设就是你所需要的一切”可以解决任何问题。为了证明这一点,JB 给了你两个从 $1$ 到 $n$ 的排列 $A$ 和 $B$,并希望你输出一个对 $A$ 进行元素交换操作 $(x_i, y_i)$ 的序列,使得:

  1. 每次交换的元素对都构成一个逆序对(即在执行第 $i$ 次操作时,满足 $x_i < y_i$ 且 $A_{x_i} > A_{y_i}$)。
  2. 在交换序列结束时,$A$ 变为 $B$。

或者判断这是不可能的。请通过解决这个问题来帮助 JB 证明他的信念!

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$,表示测试数据的组数。对于每组测试数据:

第一行包含一个整数 $n$ ($1 \le n \le 2021$),表示 $A$ 和 $B$ 中元素的个数。 第二行包含 $n$ 个不同的整数 $A_1, A_2, \dots, A_n$ ($1 \le A_i \le n$),表示数组 $A$。 第三行包含 $n$ 个不同的整数 $B_1, B_2, \dots, B_n$ ($1 \le B_i \le n$),表示数组 $B$。

保证所有测试数据中 $n$ 的总和不超过 $2021$。

输出格式

对于每组测试数据,如果不存在这样的序列,输出一行,包含一个整数“-1”。 否则,在第一行输出一个整数 $k$ ($0 \le k \le \frac{n(n-1)}{2}$),表示交换序列的长度。然后,输出 $k$ 行,每行包含两个整数 $x_i$ 和 $y_i$ ($1 \le x_i < y_i \le n$),表示第 $i$ 次操作 $\text{swap}(A_{x_i}, A_{y_i})$。

样例

样例输入 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

-1
2
1 2
2 4
7
7 8
6 7
5 6
4 5
3 4
2 3
1 2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.