QOJ.ac

QOJ

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

#11577. 逆序对

Statistics

Byteasar 发现了一类可以用排列表示的无向图。令 $V = \{1, 2, \dots, n\}$ 为顶点集。图的描述由集合 $V$ 的一个排列 $a_1, a_2, \dots, a_n$(即 $V$ 中不同数字的一个序列)给出。如果数对 $(i, j)$ 在排列中构成一个逆序对,即 $i < j$ 且 $a_i > a_j$,则顶点 $a_i$ 和 $a_j$ 之间有一条边。

例如,令 $n = 4$ 并考虑排列 $2, 3, 1, 4$。从该排列中我们得到以下图形:

Byteasar 想验证他发明的这种表示法是否真的有用。他决定编写一个程序来找出图中所有的连通分量。回想一下,如果存在一个以 $u$ 开始并以 $v$ 结束的顶点序列,使得序列中每两个相邻的顶点之间都有边相连,则称两个顶点 $u, v \in V$ 属于同一个连通分量。在我们的例子中,有两个连通分量:$\{1, 2, 3\}$ 和 $\{4\}$。

请帮助 Byteasar!

输入格式

输入的第一行包含一个整数 $n$ ($1 \le n \le 1\,000\,000$),表示图的顶点数。第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$。

输出格式

输出的第一行应包含图中连通分量的数量。记此数为 $m$。接下来的 $m$ 行,每行应包含一个连通分量的描述。首先应写出一个数字 $k$:该连通分量的大小。然后,写出该连通分量中按升序排列的顶点编号。连通分量的排列顺序应使得各分量中最小的节点编号构成一个递增序列。换句话说,如果 $S$ 和 $S'$ 是两个连通分量,$u \in S, v \in S'$ 分别是它们中编号最小的节点,且 $u < v$,则分量 $S$ 应排在 $S'$ 之前。

样例

输入 1

4
2 3 1 4

输出 1

2
3 1 2 3
1 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.