QOJ.ac

QOJ

Límite de tiempo: 1.0 s Límite de memoria: 512 MB Puntuación total: 100

#10949. 梯子游戏

Estadísticas

考虑一种我们需要公平且随机地将 $n$ 份礼物分给 $n$ 个人的情况。为此,亚洲流行一种古老的技术,通常用于表示随机排列。中国人称之为“画鬼脚”(Ghost Leg),日本人称之为“阿弥陀签”(あみだくじ),韩国人称之为“梯子游戏”(사다리타기)。我们首先从一些术语开始。该梯子由若干垂直杆和连接两个相邻垂直杆的水平横杆组成。从每根垂直杆的顶部开始,路径沿着梯子按以下三个步骤追踪:

  1. 当沿着垂直杆向下追踪时,直到到达第一根横杆的端点,然后沿着横杆继续。
  2. 当沿着横杆追踪时,直到到达横杆的另一端,然后继续沿着垂直杆向下。
  3. 重复步骤 1 和步骤 2,直到到达某根垂直杆的底部。

图 D.1(a) 展示了一个具有三根垂直杆和三根横杆的梯子 $L$。垂直杆从左到右编号为 $1, \dots, n$。追踪这三根垂直杆的路径分别如图 (b)、(c) 和 (d) 所示。输入 $(1, 2, 3)$ 最终被梯子 $L$ 置换为 $\pi_L = (3, 2, 1)$。注意,我们不允许两根紧邻的水平横杆在某一点相交的情况(如图 D.1(e) 中的 $w$),因为在该点没有唯一的追踪路径。

图 D.1:追踪梯子。

给定一个实现置换 $\pi_L$ 的梯子 $L$。如果移除某根横杆后置换 $\pi_L$ 保持不变,则称该横杆对于 $\pi_L$ 是冗余的。你的任务是找出所有对于 $\pi_L$ 冗余的横杆。这等同于通过从 $L$ 中移除所有冗余横杆来构建一个极小梯子。在 $L$ 的极小梯子中,移除任何一根横杆都会导致与 $\pi_L$ 不同的置换。

输入格式

程序从标准输入读取数据。输入的第一行包含一个整数 $n$ ($3 \le n \le 50$),表示垂直杆的数量。垂直杆从左到右排列。令 $d_{i,j}$ 表示第 $i$ 根和第 $(i+1)$ 根杆之间从上往下数的第 $j$ 根横杆的深度,它是一个介于 1 到 1,000 之间的整数。在接下来的 $n-1$ 行中,第 $i$ 行包含深度序列 $d_{i,j}$,其中 $1 \le i < n$,$d_{i,j} \ge 1$ 且 $d_{i,j} < d_{i,j+1}$。注意,该深度序列以零 (0) 结尾,0 不是深度,仅作为序列结束的标记。参见图 D.2 进行说明。

图 D.2:梯子中相邻杆之间横杆的深度。

输出格式

程序将结果输出到标准输出。打印一组在 $\pi_L$ 的极小梯子中保留的横杆。第一行应包含 $k$,即极小梯子中保留的横杆数量。接下来的 $k$ 行,每行应包含极小梯子中某根横杆的 $d_{i,j}$ 的两个索引 $i$ 和 $j$。注意,极小梯子不唯一。

样例

样例输入 1

6
2 5 8 0
6 0
1 10 0
4 6 11 0
5 7 9 0

样例输出 1

8
3 1
4 1
5 1
2 1
4 2
1 3
3 2
4 3

样例输入 2

5
2 6 9 0
3 7 8 11 0
2 4 9 0
6 10 0

样例输出 2

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

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.