B. Split Sort

B. Split Sort

You are given a permutation p1,p2,,pn of integers 1 to n.

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

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

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

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

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

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t1000). The description of the test cases follows.

The first line of each test case contains one integer n (1n100000).

The second line of each test case contains n integers p1,p2,,pn (1pin). It is guaranteed that p1,p2,,pn is a permutation.

It is guaranteed that the sum of n over all test cases does not exceed 100000.

Output

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

Example

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

Note

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

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

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

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

 

解题思路

  首先很明显x可以从2枚举到n依次进行操作,那么至少n1次就可以将整个序列变成升序。因此很自然会想到某些x是否没有必要进行操作。

  当x2枚举到k并完成操作后,此时恰好有ai=i,i[1,k1]。接着对x=k+1进行操作,而改变的地方只有将ak变成k,让kk+1的前面。如果kk+1的前面,是否可以不进行x=k+1的操作呢?可以发现如果跳过x=k+1而对x=k+2进行操作,那么就会得到ai=i,i[1,k+1]。因此如果发现kk+1的前面,那么我们就可以跳过x=k+1的操作。而如果kk+1的后面,那么必须要进行x=k+1的操作,否则永远无法将k变到k+1的前面。

  因此记录初始时数组中每个数i的下标pi,然后从小到大枚举每个数i,如果发现pi<pi+1,说明可以跳过x=i+1这个操作,否则进行。

  AC代码如下:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 1e5 + 10;
 7 
 8 int p[N];
 9 
10 void solve() {
11     int n;
12     scanf("%d", &n);
13     for (int i = 0; i < n; i++) {
14         int x;
15         scanf("%d", &x);
16         p[x] = i;
17     }
18     int ret = 0;
19     for (int i = 1; i < n; i++) {
20         if (p[i + 1] < p[i]) ret++;
21     }
22     printf("%d\n", ret);
23 }
24 
25 int main() {
26     int t;
27     scanf("%d", &t);
28     while (t--) {
29         solve();
30     }
31     
32     return 0;
33 }
复制代码

 

参考资料

  Pinely Round 2 Editorial:https://codeforces.com/blog/entry/119902

posted @   onlyblues  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-08-31 石子合并
Web Analytics
点击右上角即可分享
微信分享提示