CF1863B.Split Sort

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 (2≤x≤n2 \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).

给你一个 11 到 nn 的排列†^{\dagger} p1,p2,…,pnp_1, p_2, \ldots, p_n。

你可以通过多次(可能为零次)执行以下操作来改变当前排列:

  • 选择某个 xx(满足 2≤x≤n2 \le x \le n);
  • 按如下方式构造一个新排列:
    • 首先,按原顺序写出 pp 中所有小于 xx 的元素;
    • 然后,按原顺序写出 pp 中所有大于或等于 xx 的元素;
  • 将 pp 替换为该新构造的排列。

例如,若当前排列为 [6,4,3,5,2,1][6, 4, 3, 5, 2, 1],且你选择 x=4x = 4,则你首先写出 [3,2,1][3, 2, 1],再在其后追加 [6,4,5][6, 4, 5]。因此初始排列将被替换为 [3,2,1,6,4,5][3, 2, 1, 6, 4, 5]。

求使 pi=ip_i = i(对所有 i=1,2,…,ni = 1, 2, \ldots, n)成立所需的最少操作次数。可以证明,这一目标总能实现。

†^{\dagger} 长度为 nn 的排列是指由 11 到 nn 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数组中 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001 \le t \le 1000). The description of the test cases follows.

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

The second line of each test case contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \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 100 000100\,000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10001 \le t \le 1000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤100 0001 \le n \le 100\,000)。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \le p_i \le n)。保证 p1,p2,…,pnp_1, p_2, \ldots, p_n 是一个排列。

保证所有测试用例的 nn 值之和不超过 100 000100\,000。

输出格式

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

对于每个测试用例,在单独的一行上输出答案。

输入输出样例

  • 输入#1

    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

    输出#1

    0
    1
    4
    1
    7

说明/提示

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].

在第一个测试用例中,n=1n = 1 且 p1=1p_1 = 1,因此无需任何操作。

在第二个测试用例中,我们可以选择 x=2x = 2,从而立即得到 p1=1p_1 = 1、p2=2p_2 = 2。

在第三个测试用例中,我们可通过以下方式实现最少的操作次数:

  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]。

输入解题思路,AI测评打分。不知道怎么写?

首页