QOJ.ac

QOJ

Límite de tiempo: 1.0 s Límite de memoria: 256 MB Puntuación total: 100 Hackeable ✓

#12115. 薯片蘸酱

Estadísticas

Aibar 发明了一种新的棋盘游戏并邀请你来尝试。游戏包含 $n$ 枚棋子,每枚棋子都有一个从 $1$ 到 $n$ 的唯一等级。Aibar 已经将棋子按某种初始顺序排列在一条直线上,并告诉了你最终的顺序。

你的任务是逐个移动棋子以达到最终顺序。当将一枚棋子移动到新位置时,原位置和新位置之间的所有棋子在当前移动期间被视为“激活”状态。

游戏只有一条规则:要将等级为 $x$ 的棋子移动到新位置,被激活的棋子中等级高于 $x$ 的数量必须等于等级低于 $x$ 的数量。

例如,假设棋子排列为 $[8, 4, 5, 6, 3, 2, 7, 1]$。你可以将第 $2$ 个位置的棋子向右移动 $4$ 个位置。此时棋子 $\{5, 6, 3, 2\}$ 被激活,其中 $5$ 和 $6$ 的等级高于 $4$,而 $3$ 和 $2$ 的等级低于 $4$。两者的数量相等,因此该移动合法,新的排列将变为 $[8, 5, 6, 3, 2, 4, 7, 1]$。

请找到一个能达到最终顺序的移动序列。

输入格式

第一行包含两个数字 $n$ 和 $t$ ($1 \le n \le 200, 1 \le t \le 2$),分别表示棋子数量和要求的输出类型。

接下来的两行分别包含一个 $1$ 到 $n$ 的排列,表示初始顺序和最终顺序。

输出格式

如果可以获胜(即能将棋子排列成最终顺序),输出 YES,否则输出 NO

如果 $t = 2$ 且可以获胜,则在下一行输出 $m$ ($0 \le m \le 10^6$),表示所需的移动次数。

接下来的 $m$ 行,每行包含两个数字 $p$ 和 $k$ ($1 \le p \le n, 1 \le k \le n - i$ 或 $1 \le -k \le i - 1$),描述相应的移动:将位于位置 $p$ 的棋子向右移动 $k$ 个位置。如果 $k < 0$,则将棋子向左移动 $|k|$ 个位置。

可以证明,在给定约束条件下,只要存在获胜策略,就一定存在一个长度不超过 $10^6$ 的移动序列。你可以输出任意一个这样的序列。

子任务

本题由 $8$ 个子任务组成,满足以下约束:

  1. $n \le 8$。分值 $7$ 分。
  2. $n \le 11$。分值 $12$ 分。
  3. 保证存在一个数 $k$,使得 $a_i = i$ ($1 \le i \le k-1$),$a_j = j$ ($k+2 \le i \le n$) 且 $a_k = k + 1, a_{k+1} = k$,其中 $a_i$ 表示初始顺序中第 $i$ 个棋子的等级。分值 $9$ 分。
  4. $t = 1$。分值 $12$ 分。
  5. $n \le 25$。分值 $11$ 分。
  6. $n \le 50$。分值 $18$ 分。
  7. $n \le 100$。分值 $17$ 分。
  8. 原始问题约束。分值 $14$ 分。

样例

样例输入 1

3 1
1 2 3
3 2 1

样例输出 1

NO

样例输入 2

4 2
3 2 4 1
4 2 1 3

样例输出 2

YES
3
2 2
1 2
4 -2

样例输入 3

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

样例输出 3

YES
5
2 2
6 -2
2 2
1 2
8 -6

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.