QOJ.ac

QOJ

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

#1904. 排列变换

统计

Fedor 在置换变换部门工作。今天 Fedor 需要解决以下问题:他需要使用至多 $n^3$ 次 $k$-转移操作,将整数 $1, 2, \dots, n$ 的置换 $[p_1, p_2, \dots, p_n]$ 变换为置换 $[q_1, q_2, \dots, q_n]$。

考虑一个长度为 $n$ 的数组。参数为 $(a, b)$ 的 $k$-转移操作定义如下:将数组中从索引 $a$ 开始的 $k$ 个连续元素切下,并将其插入到索引 $b$ 的位置。

更正式地说:考虑一个数组 $[t_1, t_2, \dots, t_n]$ 和两个整数 $a$ 和 $b$ ($1 \le a, b \le n-k+1$)。让我们创建一个临时数组 $[r_1, r_2, \dots, r_{n-k}]$,由数字 $[t_1, t_2, \dots, t_{a-1}, t_{a+k}, t_{a+k+1}, \dots, t_n]$ 组成。那么对于数组 $t$,参数为 $(a, b)$ 的 $k$-转移的结果是一个由数字 $[r_1, r_2, \dots, r_{b-1}, t_a, t_{a+1}, \dots, t_{a+k-1}, r_b, r_{b+1}, \dots, r_{n-k}]$ 组成的数组。

Fedor 不知道如何解决这个任务,所以他请求你的帮助!

你需要解决 $t$ 组测试用例。

输入格式

第一行包含一个整数 $t$ ($1 \le t \le 100$),表示测试用例的数量。

每个测试用例包含三行。第一行包含两个整数 $n$ 和 $k$ ($1 \le k \le n \le 100$)。

第二行包含 $n$ 个不同的整数 $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$),表示置换 $p$。

第三行包含 $n$ 个不同的整数 $q_1, q_2, \dots, q_n$ ($1 \le q_i \le n$),表示置换 $q$。

保证所有测试用例中 $n$ 的总和不超过 $100$。

输出格式

为每个测试用例打印答案。请按以下格式输出单个测试用例的答案。

如果无法通过 $k$-转移从置换 $p_1, p_2, \dots, p_n$ 得到置换 $q_1, q_2, \dots, q_n$,则打印一行包含单词 “NO”。

否则,第一行打印 “YES”。

第二行必须包含一个整数 $m$ —— 为从置换 $p$ 得到置换 $q$ 所执行的 $k$-转移次数 ($0 \le m \le n^3$)。注意,你不需要最小化 $m$。保证如果可以通过 $k$-转移从置换 $p$ 得到置换 $q$,则存在一个至多需要 $n^3$ 次操作的解。

接下来的 $m$ 行,每行包含两个整数 —— 对应 $k$-转移的参数 $a$ 和 $b$。

样例

样例输入 1

3
2 1
2 1
2 1
4 2
1 2 3 4
1 2 4 3
3 2
2 1 3
1 3 2

样例输出 1

YES
0
NO
YES
2
1 2
1 2

说明

在第三个测试用例中,存在另一种从置换 $p$ 得到置换 $q$ 的方法 —— 即参数为 $a = 2, b = 1$ 的单次 $k$-转移。

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.