QOJ.ac

QOJ

Limite de temps : 3 s Limite de mémoire : 1024 MB Points totaux : 100 Difficulté: [afficher] Hackable ✓

#6511. 平衡序列

Statistiques

在前往幻想乡(Gensokyo)旅行后,Little Cyan Fish 得到了两个序列 $a_1$ 和 $a_2$。每个序列包含 $n$ 个整数,范围在 $1$ 到 $2n$ 之间。这 $2n$ 个整数两两互不相同。

他想要将 $a_1, a_2$ 转换为 $b_1, b_2$。不幸的是,这些序列拥有一个自平衡系统,因此他唯一能执行的操作是选择四个整数 $(x_1, x_2, y_1, y_2)$ 并交换元素 $a_{x_1, x_2}$ 和 $a_{y_1, y_2}$。为了保护自平衡系统,这些选定的整数必须满足:

  • $x_1, y_1 \in [1, 2]$ 且 $x_2, y_2 \in [1, n]$。
  • $x_2 \neq y_2$。
  • $a_{x_1, x_2} > a_{3-x_1, x_2}$。
  • $a_{y_1, y_2} > a_{3-y_1, y_2}$。

Little Cyan Fish 想知道他是否可以将 $a_1, a_2$ 转换为 $b_1, b_2$,因此他向你寻求帮助。如果可行,你需要提供一个方案来引导他。

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$ ($1 \le T \le 10^5$),表示测试数据的组数。

对于每组测试数据,第一行包含一个整数 $n$ ($2 \le n \le 2 \times 10^3$),表示 $a_1, a_2, b_1$ 和 $b_2$ 的大小。

接下来一行包含 $n$ 个元素,描述 $a_1$ ($1 \le a_{1,i} \le 2n$)。 接下来一行包含 $n$ 个元素,描述 $a_2$ ($1 \le a_{2,i} \le 2n$)。序列 $a_1$ 和 $a_2$ 中的所有 $2n$ 个整数两两互不相同。 接下来一行包含 $n$ 个元素,描述 $b_1$ ($1 \le b_{1,i} \le 2n$)。 接下来一行包含 $n$ 个元素,描述 $b_2$ ($1 \le b_{2,i} \le 2n$)。序列 $b_1$ 和 $b_2$ 中的所有 $2n$ 个整数两两互不相同。

保证所有测试数据的 $n^2$ 之和不超过 $4 \times 10^6$。

输出格式

如果无法将数组 $a_1$ 和 $a_2$ 转换为 $b_1$ 和 $b_2$,输出一行,包含单个整数 $-1$。

否则,输出一个整数 $s$ ($s \in [0, 5n]$),表示将 $a_1$ 和 $a_2$ 转换为 $b_1$ 和 $b_2$ 所需的步数。 在接下来的 $s$ 行中,每行输出四个数字 $x_1, x_2, y_1, y_2$ ($1 \le x_1, y_1 \le 2, 1 \le x_2, y_2 \le n$),表示在这一步中交换 $a_{x_1, x_2}$ 和 $a_{y_1, y_2}$。

可以证明,如果转换是可能的,那么它可以在 $5n$ 步内完成。

样例

输入 1

2
2
1 2
3 4
4 3
2 1
3
1 2 4
3 5 6
1 2 4
5 3 6

输出 1

-1
1
2 2 2 1

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.