QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 512 MB Points totaux : 100 Difficulté: [afficher]

#5147. 栈排序

Statistiques

给定一个包含 $n$ 个数字的排列 $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n, a_i \neq a_j$ 当 $i \neq j$ 时)。

你想要使用 $m$ 个栈来对这些数字进行排序。具体来说,你需要完成以下任务: 初始时,所有栈均为空。你需要按照 $a_1, a_2, \dots, a_n$ 的顺序,依次将每个数字 $a_i$ 推入 $m$ 个栈中的某一个的顶部。在将所有数字推入栈中后,你需要以一种巧妙的顺序从栈中弹出所有元素,使得弹出的第一个数字为 $1$,第二个数字为 $2$,以此类推。如果你从某个栈 $S$ 中弹出了一个元素,在 $S$ 变为空之前,你不能从其他栈中弹出任何元素。

请问完成该任务所需的最小 $m$ 是多少?

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 10^5$),表示测试用例的数量。

对于每个测试用例,第一行包含一个正整数 $n$ ($1 \le n \le 5 \times 10^5$)。下一行包含 $n$ 个整数 $a_1, \dots, a_n$ ($1 \le a_i \le n$),表示该排列。保证 $a_1, \dots, a_n$ 构成一个排列,即当 $i \neq j$ 时 $a_i \neq a_j$。

保证所有测试用例中 $n$ 的总和不超过 $5 \times 10^5$。

输出格式

对于每个测试用例,输出一行,包含最小可能的 $m$。

样例

输入 1

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

输出 1

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.