CF1792C.Min Max Sort

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a permutation pp of length nn (a permutation of length nn is an array of length nn in which each integer from 11 to nn occurs exactly once).

You can perform the following operation any number of times (possibly zero):

  1. choose two different elements xx and yy and erase them from the permutation;
  2. insert the minimum of xx and yy into the permutation in such a way that it becomes the first element;
  3. insert the maximum of xx and yy into the permutation in such a way that it becomes the last element.

For example, if p=[1,5,4,2,3]p = [1, 5, 4, 2, 3] and we want to apply the operation to the elements 33 and 55, then after the first step of the operation, the permutation becomes p=[1,4,2]p = [1, 4, 2]; and after we insert the elements, it becomes p=[3,1,4,2,5]p = [3, 1, 4, 2, 5].

Your task is to calculate the minimum number of operations described above to sort the permutation pp in ascending order (i. e. transform pp so that p1<p2<⋯<pnp_1 \lt p_2 \lt \dots \lt p_n).

给你一个长度为 nn 的排列 pp(长度为 nn 的排列是指一个长度为 nn 的数组,其中恰好包含从 11 到 nn 的每个整数各一次)。

你可以执行以下操作任意多次(包括零次):

  1. 选择两个不同的元素 xx 和 yy,并将它们从排列中删除;
  2. 将 min⁡(x,y)\min(x, y) 插入排列中,使其成为第一个元素;
  3. 将 max⁡(x,y)\max(x, y) 插入排列中,使其成为最后一个元素。

例如,若 p=[1,5,4,2,3]p = [1, 5, 4, 2, 3],且我们对元素 33 和 55 执行该操作,则操作第一步后排列变为 p=[1,4,2]p = [1, 4, 2];插入元素后,排列变为 p=[3,1,4,2,5]p = [3, 1, 4, 2, 5]。

你的任务是计算使排列 pp 按升序排列(即变换 pp 使得 p1<p2<⋯<pnp_1 \lt p_2 \lt \dots \lt p_n)所需的上述操作的最少次数。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of the test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of elements in the permutation.

The second line of the test case contains nn distinct integers from 11 to nn — the given permutation pp.

The sum of nn over all test cases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 排列中元素的个数。

每个测试用例的第二行包含 nn 个互不相同的整数,取值范围为 11 到 nn —— 给定的排列 pp。

所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the minimum number of operations described above to sort the array pp in ascending order.

对于每个测试用例,输出一个整数——将数组 pp 按升序排序所需的上述操作的最少次数。

输入输出样例

  • 输入#1

    4
    5
    1 5 4 2 3
    3
    1 2 3
    4
    2 1 4 3
    6
    5 2 4 1 6 3

    输出#1

    2
    0
    1
    3

说明/提示

In the first example, you can proceed as follows:

  1. in the permutation p=[1,5,4,2,3]p = [1, 5, 4, 2, 3], let's choose the elements 44 and 22, then, after applying the operation, the permutation becomes p=[2,1,5,3,4]p = [2, 1, 5, 3, 4];
  2. in the permutation p=[2,1,5,3,4]p = [2, 1, 5, 3, 4], let's choose the elements 11 and 55, then, after applying operation, the permutation becomes p=[1,2,3,4,5]p = [1, 2, 3, 4, 5].

在第一个例子中,你可以按如下步骤进行:

  1. 在排列 p=[1,5,4,2,3]p = [1, 5, 4, 2, 3] 中,选择元素 44 和 22,应用操作后,排列变为 p=[2,1,5,3,4]p = [2, 1, 5, 3, 4];
  2. 在排列 p=[2,1,5,3,4]p = [2, 1, 5, 3, 4] 中,选择元素 11 和 55,应用操作后,排列变为 p=[1,2,3,4,5]p = [1, 2, 3, 4, 5]。

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

首页