QOJ.ac

QOJ

Time Limit: 6 s Memory Limit: 1024 MB Total points: 100

#5594. 贪心递增子序列

Statistics

Jimmy 的家庭作业是为一个给定的序列 $a_1, a_2, \dots, a_n$ 寻找一个长递增子序列。但这个序列实在太长了!Jimmy 不知道如何有效地完成这项任务。

因此,Jimmy 采取了一种贪心策略。他首先选取序列中的第一个数。然后,他重复执行以下规则,直到无法继续为止:选取序列中下一个比他刚刚选取的数更大的数。

更准确地说,Jimmy 选取的子序列 $a_{i_1}, a_{i_2}, \dots, a_{i_k}$ 满足:

  • $i_1 = 1$
  • 对于每个 $1 \le j < k$,$i_{j+1}$ 是大于 $i_j$ 的最小下标,使得 $a_{i_j} < a_{i_{j+1}}$
  • 对于所有 $\ell > i_k$,都有 $a_{i_k} \ge a_\ell$

Jimmy 意识到这可能无法产生一个很长的子序列。为了帮助他找到其他子序列,他从给定序列中移除 $a_{i_1}, a_{i_2}, \dots, a_{i_k}$,并在剩余序列上再次使用他的贪心算法寻找另一个递增子序列。他重复此过程,直到用尽原始序列中的所有数字。

但即使这样对 Jimmy 来说也太累了,所以他请求你帮助他,找出通过重复应用上述贪心过程并移除所得子序列,直到给定序列为空时所产生的所有序列。

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 2 \times 10^5$),表示原始序列的长度。 第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$)。

输出格式

第一行输出产生的序列总数 $s$。接下来的 $s$ 行包含这些序列,第 $i$ 行包含在第 $i$ 次应用贪心算法时形成的递增子序列。

样例

样例输入 1

7
2 2 1 5 3 4 6

样例输出 1

3
2 5 6
2 3 4
1

样例输入 2

7
8 6 7 5 3 0 9

样例输出 2

5
8 9
6 7
5
3
0

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.