luogu#P9677. [ICPC2022 Jinan R] Stack Sort

[ICPC2022 Jinan R] Stack Sort

题目描述

You are given a permutation with nn numbers, $a_1, a_2, \dots, a_n (1\leq a_i\leq n, a_i\neq a_j\textrm{ when }i\neq j)$.

You want to sort these numbers using mm stacks. Specifically, you should complete the following task:

Initially, all stacks are empty. You need to push each number aia_i to the top of one of the mm stacks one by one, in the order of a1,a2,,ana_1,a_2,\ldots, a_n. After pushing all numbers in the stacks\textbf{After pushing all numbers in the stacks}, you pop all the elements from the stacks in a clever order so that the first number you pop is 11, the second number you pop is 22, and so on. If you pop an element from a stack SS, you cannot pop any element from the other stacks until SS becomes empty.

What is the minimum possible mm to complete the task?

输入格式

The first line contains one integer T (1T105)T~(1\le T \le 10^5), the number of test cases.

For each test case, the first line contains one positive integer n (1n5×105)n~(1\le n \le 5 \times 10^5). The next line contains nn integers a1,,an (1ain)a_1,\ldots, a_n~(1 \le a_i\le n) denoting the permutation. It is guaranteed that a1,,ana_1,\ldots, a_n form a permutation, i.e. aiaja_i\neq a_j for iji \neq j.

It is guaranteed that the sum of nn over all test cases is no more than 5×1055\times 10^5.

输出格式

For each test case, output the minimum possible mm in one line.

题目大意

你有一个 nn 项的数组 aamm 个栈,满足:

  • 1ain1\leq a_i\le n,每一项各不相同。
  • 对于 i=1,2...ni=1,2...n,使 aia_i 成为任意一个栈的栈顶。
  • 在每一项入栈后有一种方法使得 1,2...,n1,2...,n 可以按照递增顺序依次出栈。

mm 的最小值。

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

3
1
4