CF BUDDY
← Problems·

1863B · Split Sort

1100 · greedy, math, sortings

Problem: You are given a permutation^{\dagger} p1,p2,,pnp_1, p_2, \ldots, p_n of integers 11 to nn.

You can change the current permutation by applying the following operation several (possibly, zero) times:

  • choose some xx (2xn2 \le x \le n);
  • create a new permutation by: first, writing down all elements of pp that are less than xx, without changing their order; second, writing down all elements of pp that are greater than or equal to xx, without changing their order;
  • replace pp with the newly created permutation.

For example, if the permutation used to be [6,4,3,5,2,1][6, 4, 3, 5, 2, 1] and you choose x=4x = 4, then you will first write down [3,2,1][3, 2, 1], then append this with [6,4,5][6, 4, 5]. So the initial permutation will be replaced by [3,2,1,6,4,5][3, 2, 1, 6, 4, 5].

Find the minimum number of operations you need to achieve pi=ip_i = i for i=1,2,,ni = 1, 2, \ldots, n. We can show that it is always possible to do so.

^{\dagger} A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t10001 \le t \le 1000). The description of the test cases follows.

The first line of each test case contains one integer nn (1n1000001 \le n \le 100\,000).

The second line of each test case contains nn integers p1,p2,,pnp_1, p_2, \ldots, p_n (1pin1 \le p_i \le n). It is guaranteed that p1,p2,,pnp_1, p_2, \ldots, p_n is a permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 100000100\,000.

Output Format: For each test case, output the answer on a separate line.

Note: In the first test case, n=1n = 1 and p1=1p_1 = 1, so there is nothing left to do.

In the second test case, we can choose x=2x = 2 and we immediately obtain p1=1p_1 = 1, p2=2p_2 = 2.

In the third test case, we can achieve the minimum number of operations in the following way:

  1. x=4x = 4: [6,4,3,5,2,1][3,2,1,6,4,5][6, 4, 3, 5, 2, 1] \rightarrow [3, 2, 1, 6, 4, 5];
  2. x=6x = 6: [3,2,1,6,4,5][3,2,1,4,5,6][3, 2, 1, 6, 4, 5] \rightarrow [3, 2, 1, 4, 5, 6];
  3. x=3x = 3: [3,2,1,4,5,6][2,1,3,4,5,6][3, 2, 1, 4, 5, 6] \rightarrow [2, 1, 3, 4, 5, 6];
  4. x=2x = 2: [2,1,3,4,5,6][1,2,3,4,5,6][2, 1, 3, 4, 5, 6] \rightarrow [1, 2, 3, 4, 5, 6].

Sample Cases

Case 1

Input

5
1
1
2
2 1
6
6 4 3 5 2 1
3
3 1 2
19
10 19 7 1 17 11 8 5 12 9 4 18 14 2 6 15 3 16 13

Output

0
1
4
1
7

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback