QOJ.ac

QOJ

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

#10540. Pair Sorting

Statistics

有 $n$ 个排成一行的箱子和 $2n$ 个地上的球。球的编号从 $1$ 到 $n$,每个编号 $i$ ($1 \le i \le n$) 的球恰好有两个。对于 $1 \le i \le n$,第 $i$ 个箱子记为 $B_i$,每个箱子 $B_i$ 最多可容纳两个球。初始时,对于 $1 \le i \le n$,箱子 $B_i$ 中装有两个编号为 $n + 1 - i$ 的球。初始配置见下图 F.1。

图 F.1. 箱子的初始配置

你可以交换相邻箱子中的两个球,这算作一次交换操作。注意,箱子不是栈,对于相邻的箱子 $B_i$ 和 $B_{i+1}$,你可以交换 $B_i$ 中的两个球之一与 $B_{i+1}$ 中的两个球之一。见下图 F.2。该图展示了两次交换操作。

图 F.2. 相邻箱子之间的交换操作

通过这些交换操作,你需要将球排序。排序的结果要求对于 $1 \le i \le n$,箱子 $B_i$ 中必须包含两个编号为 $i$ 的球。特别地,交换操作的总次数不得超过 $Bound$,其中 $Bound$ 是关于 $n$ 的函数,具体为 $Bound = 0.7n^2$。

给定 $n$ 个箱子和 $2n$ 个球,编写一个程序找到一种球的排序方法,使得交换操作的总次数不超过 $Bound = 0.7n^2$。

输入格式

程序从标准输入读取数据。输入仅包含一行,包含一个整数 $n$ ($3 \le n \le 100$),表示有 $n$ 个箱子和 $2n$ 个球。

输出格式

程序向标准输出写入数据。设 $S$ 为你所采用的排序方法中交换操作的总次数。请打印 $S + 1$ 行。第一行包含 $S$。接下来的 $S$ 行,每行包含三个整数 $j$、$a$ 和 $b$,表示在箱子 $B_j$ 中的球 $a$ 与箱子 $B_{j+1}$ 中的球 $b$ 之间进行一次交换操作,其中 $1 \le j \le n - 1$ 且 $1 \le a, b \le n$。排序方法中的交换操作应按顺序逐行打印。$S$ 必须满足 $S \le 0.7n^2$。

样例

样例输入 1

3

样例输出 1

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

样例输入 2

3

样例输出 2

5
1 3 2
2 3 1
1 3 1
2 3 1
1 2 1

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.