QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100

#5277. 有限交换

Statistics

Lina 正在玩 $n$ 个排成一行的方块。每个方块上都写有一个 $1$ 到 $n$ 之间的整数,且 $1$ 到 $n$ 中的每个整数恰好出现在一个方块上。

初始时,从左到右方块上的数字为 $a_1, a_2, \dots, a_n$。Lina 希望将方块上的数字变为从左到右 $b_1, b_2, \dots, b_n$。

Lina 可以交换任意两个相邻的方块,但前提是这两个方块上的数字之差至少为 $2$。该操作最多可以执行 $20\,000$ 次。

请找出任意一种能将初始配置转换为目标配置的交换序列,如果无法实现,请报告无解。

输入格式

第一行包含一个整数 $n$ —— 方块的数量 ($1 \le n \le 100$)。

第二行包含 $n$ 个不同的整数 $a_1, a_2, \dots, a_n$ —— 从左到右的初始数字 ($1 \le a_i \le n$)。

第三行包含 $n$ 个不同的整数 $b_1, b_2, \dots, b_n$ —— 从左到右的目标数字 ($1 \le b_i \le n$)。

输出格式

如果无法从初始配置得到目标配置,输出一个整数 $-1$。

否则,在第一行输出一个整数 $k$ —— 交换序列的次数 ($0 \le k \le 20\,000$)。

在第二行输出 $k$ 个整数 $s_1, s_2, \dots, s_k$,按顺序描述操作 ($1 \le s_i \le n - 1$)。整数 $s_i$ 表示“交换从左往右第 $s_i$ 个方块和第 $s_i + 1$ 个方块”。

你不需要找到最短的方案,任何满足约束条件的方案均可。

样例

样例输入 1

5
1 3 5 2 4
3 5 1 4 2

样例输出 1

5
2 1 2 4 1

样例输入 2

4
1 2 3 4
4 3 2 1

样例输出 2

-1

说明

在第一个样例测试中,配置的变化过程如下:

$1\ 3\ 5\ 2\ 4 \to 1\ 5\ 3\ 2\ 4 \to 5\ 1\ 3\ 2\ 4 \to 5\ 3\ 1\ 2\ 4 \to 5\ 3\ 1\ 4\ 2 \to 3\ 5\ 1\ 4\ 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.