luogu#P9677. [ICPC2022 Jinan R] Stack Sort
[ICPC2022 Jinan R] Stack Sort
题目描述
You are given a permutation with 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 stacks. Specifically, you should complete the following task:
Initially, all stacks are empty. You need to push each number to the top of one of the stacks one by one, in the order of . , you pop all the elements from the stacks in a clever order so that the first number you pop is , the second number you pop is , and so on. If you pop an element from a stack , you cannot pop any element from the other stacks until becomes empty.
What is the minimum possible to complete the task?
输入格式
The first line contains one integer , the number of test cases.
For each test case, the first line contains one positive integer . The next line contains integers denoting the permutation. It is guaranteed that form a permutation, i.e. for .
It is guaranteed that the sum of over all test cases is no more than .
输出格式
For each test case, output the minimum possible in one line.
题目大意
你有一个 项的数组 和 个栈,满足:
- ,每一项各不相同。
- 对于 ,使 成为任意一个栈的栈顶。
- 在每一项入栈后有一种方法使得 可以按照递增顺序依次出栈。
求 的最小值。
3
3
1 2 3
3
3 2 1
5
1 4 2 5 3
3
1
4