QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 256 MB Puntuación total: 100

#1646. 磁碟排序

Estadísticas

你擁有 $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$),代表最初放置在桿子上的圓盤顏色。這 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.