QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100

#1646. 磁盘排序

Statistics

你有 $n + 1$ 根杆子和 $3n$ 个圆盘。最初,前 $n$ 根杆子中的每一根都恰好包含 3 个圆盘。每个圆盘都有 $n$ 种颜色之一(用从 1 到 $n$ 的数字标识)。此外,每种颜色恰好有 3 个圆盘。第 $n+1$ 根杆子是空的。

在每一步中,我们可以选择两根杆子 $a$ 和 $b$ ($a \neq b$),使得 $a$ 至少有 1 个圆盘,且 $b$ 最多有 2 个圆盘,然后将杆子 $a$ 最顶端的圆盘移动到杆子 $b$ 的顶部。注意,任何时候任何杆子都不允许包含超过 3 个圆盘。

你的目标是对圆盘进行排序。更具体地说,你需要执行若干次操作(可能为 0 次),使得最后前 $n$ 根杆子中的每一根都恰好包含 3 个相同颜色的圆盘,且第 $n+1$ 根杆子为空。

请找出一个在最多 $6n$ 次操作内对圆盘进行排序的方案。可以证明,在此条件下,解总是存在的。如果存在多种方案,输出其中任意一种即可。

输入格式

输入的第一行包含一个正整数 $n$ ($1 \le n \le 1000$)。接下来的 3 行输入包含 $n$ 个正整数 $c_{i,j}$ ($1 \le i \le 3, 1 \le j \le n, 1 \le c_{i,j} \le n$),表示最初放置在杆子上的圆盘颜色。这 3 行中的第一行表示顶行,第二行表示中间行,第三行表示底行。

输出格式

输出的第一行必须包含一个非负整数 $k$ ($0 \le k \le 6n$),表示操作次数。接下来的 $k$ 行,每行应包含两个不同的数字 $a_i, b_i$ ($1 \le a_i, b_i \le n + 1$,对于所有 $1 \le i \le k$),表示第 $i$ 次操作(如题目描述中所述)。

样例

输入 1

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

输出 1

8
3 5
3 5
2 3
2 5
2 3
5 2
5 2
5 2

输入 2

2
1 2
1 2
1 2

输出 2

0

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#310EditorialOpen题解jiangly2025-12-14 07:02:19View

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.