#P7714. 「EZEC-10」排列排序

    ID: 6616 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>模拟基础算法构造差分双指针,尺取法,two-pointer

「EZEC-10」排列排序

Description

You are given a permutation p1,p2,,pnp_1,p_2,\cdots,p_n of length nn. You need to sort it.

Each time, you may pay a cost equal to the length of the interval, i.e. rl+1r-l+1, choose any interval [l,r][l,r] in the permutation, and sort [l,r][l,r] in increasing order.

Now you can perform this operation several times until the values in pp are sorted in increasing order from 11 to nn, that is, for every ii from 11 to nn, we have pi=ip_i=i.

What is the minimum total cost?

Input Format

This problem has multiple test cases. The first line contains an integer TT, indicating the number of test cases.

For each test case:

The first line contains an integer nn.

The second line contains nn integers separated by spaces, representing the values of the permutation pp.

Output Format

Output TT lines, each containing one integer representing the answer for one test case.

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

Hint

[Sample 11 Explanation]

For the first test case, you can choose the interval [2,3][2,3] to sort.

For the second test case, you can choose the interval [1,3][1,3] to sort.

[Constraints]

For 20%20\% of the testdata, n4n\leq 4.

For another 30%30\% of the testdata, n5000\sum n\leq 5000.

For another 10%10\% of the testdata, p1=np_1=n.

For 100%100\% of the testdata, 1T,n1061\le T,\sum n\le 10^6.

Translated by ChatGPT 5