QOJ.ac

QOJ

حد الوقت: 2.0 s حد الذاكرة: 256 MB مجموع النقاط: 100 قابلة للهجوم ✓

#11540. 玩具弹珠

الإحصائيات

Busy Beaver 发现有人把他的玩具弹珠弄混了!

共有 $N$ 个容器,编号从 $1$ 到 $N$。第 $i$ 个容器中目前装有一颗颜色为 $c_i$ 的弹珠。

Busy Beaver 想要整理他的弹珠,使得第 $i$ 个容器中只装有颜色为 $i$ 的弹珠。为了达到这个目的,他可以执行以下任一操作任意多次(包括零次):

  • 交换容器 $x$ 和 $y$ 中的弹珠。执行此操作后,容器 $x$ 中的所有弹珠移动到容器 $y$,反之亦然。
  • 将容器 $y$ 中的所有弹珠移动到容器 $x$。执行此操作后,容器 $y$ 变为空,且其所有弹珠都被移动到容器 $x$。

请找出一种使用最少操作次数整理弹珠的方法。

输入格式

第一行包含一个整数 $N$ ($1 \leq N \leq 2 \cdot 10^5$),表示容器的数量。

第二行包含 $N$ 个整数 $c_1, c_2, \dots, c_N$ ($1 \leq c_i \leq N$),表示每个容器中初始的弹珠颜色。

输出格式

第一行输出一个整数 $K$,表示所需的最少操作次数。

接下来的 $K$ 行,按顺序描述操作,每行一个。每个操作应采用以下格式之一:

  • $\texttt{1}~\,x~\,y$:交换容器 $x$ 和 $y$ 中的弹珠 ($1 \leq x, y \leq N$; $x \neq y$)。
  • $\texttt{2}~\,x~\,y$:将容器 $y$ 中的所有弹珠移动到容器 $x$ ($1 \leq x, y \leq N$; $x \neq y$)。

如果存在多种以最少操作次数达到目标的方法,输出其中任意一种即可。

子任务

本题共有两个子任务:

  • ($20$ 分) 所有 $c_i$ 互不相同。
  • ($80$ 分) 无额外限制。

样例

输入格式 1

4
2 4 3 1

输出格式 1

2
1 2 4
1 1 2

说明

在第一个测试用例中,可以通过两次操作达到目标:

  • 交换容器 $2$ 和 $4$ 中的弹珠。
  • 交换容器 $1$ 和 $2$ 中的弹珠。

不可能用少于两次的操作达到目标。

输入格式 2

8
3 6 7 6 3 6 8 3

输出格式 2

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

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.