CF1838B.Minimize Permutation Subarrays

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a permutation pp of size nn. You want to minimize the number of subarrays of pp that are permutations. In order to do so, you must perform the following operation exactly once:

  • Select integers ii, jj, where 1≤i,j≤n1 \le i, j \le n, then
  • Swap pip_i and pjp_j.

For example, if p=[5,1,4,2,3]p = [5, 1, 4, 2, 3] and we choose i=2i = 2, j=3j = 3, the resulting array will be [5,4,1,2,3][5, 4, 1, 2, 3]. If instead we choose i=j=5i = j = 5, the resulting array will be [5,1,4,2,3][5, 1, 4, 2, 3].

Which choice of ii and jj will minimize the number of subarrays that are permutations?

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

An array aa is a subarray of an array bb if aa can be obtained from bb by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

给你一个长度为 nn 的排列 pp。你希望最小化 pp 中是排列的子数组的数量。为此,你必须恰好执行一次以下操作:

  • 选择整数 ii、jj,满足 1≤i,j≤n1 \le i, j \le n,然后
  • 交换 pip_i 和 pjp_j。

例如,若 p=[5,1,4,2,3]p = [5, 1, 4, 2, 3],且我们选择 i=2i = 2、j=3j = 3,则得到的新数组为 [5,4,1,2,3][5, 4, 1, 2, 3];若改为选择 i=j=5i = j = 5,则数组保持不变:[5,1,4,2,3][5, 1, 4, 2, 3]。

应如何选择 ii 和 jj,才能使是排列的子数组数量最少?

长度为 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)。

数组 aa 是数组 bb 的子数组,当且仅当 aa 可通过从 bb 的开头删除若干(可能为零或全部)元素、并从 bb 的末尾删除若干(可能为零或全部)元素而得到。

输入格式

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

The first line of each test case contains a single integer nn (3≤n≤2⋅1053 \le n \le 2\cdot 10^5) — the size of the permutation.

The next 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, all pip_i are distinct) — the elements of the permutation pp.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \le n \le 2\cdot 10^5),表示排列的长度。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \le p_i \le n,且所有 pip_i 互不相同),表示排列 pp 的元素。

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

输出格式

For each test case, output two integers ii and jj (1≤i,j≤n1 \le i, j \le n) — the indices to swap in pp.

If there are multiple solutions, print any of them.

对于每个测试用例,输出两个整数 ii 和 jj(1≤i,j≤n1 \le i, j \le n)—— 即在排列 pp 中需要交换的下标。

若存在多个解,输出任意一个即可。

输入输出样例

  • 输入#1

    8
    3
    1 2 3
    3
    1 3 2
    5
    1 3 2 5 4
    6
    4 5 6 1 2 3
    9
    8 7 6 3 2 1 4 5 9
    10
    7 10 5 1 9 8 3 2 6 4
    10
    8 5 10 9 2 1 3 4 6 7
    10
    2 3 5 7 10 1 8 6 4 9

    输出#1

    2 3
    1 1
    5 2
    1 4
    9 5
    8 8
    6 10
    5 4

说明/提示

For the first test case, there are four possible arrays after the swap:

  • If we swap p1p_1 and p2p_2, we get the array [2,1,3][2, 1, 3], which has 3 subarrays that are permutations ([1][1], [2,1][2, 1], [2,1,3][2, 1, 3]).
  • If we swap p1p_1 and p3p_3, we get the array [3,2,1][3, 2, 1], which has 3 subarrays that are permutations ([1][1], [2,1][2, 1], [3,2,1][3, 2, 1]).
  • If we swap p2p_2 and p3p_3, we get the array [1,3,2][1, 3, 2], which has 2 subarrays that are permutations ([1][1], [1,3,2][1, 3, 2]).
  • If we swap any element with itself, we get the array [1,2,3][1, 2, 3], which has 3 subarrays that are permutations ([1][1], [1,2][1, 2], [1,2,3][1, 2, 3]).

So the best swap to make is positions 22 and 33.

For the third sample case, after we swap elements at positions 22 and 55, the resulting array is [1,4,2,5,3][1, 4, 2, 5, 3]. The only subarrays that are permutations are [1][1] and [1,4,2,5,3][1, 4, 2, 5, 3]. We can show that this is minimal.

对于第一个测试用例,交换后共有四种可能的数组:

  • 若交换 p1p_1 与 p2p_2,得到数组 [2,1,3][2, 1, 3],其中包含 3 个是排列的子数组([1][1]、[2,1][2, 1]、[2,1,3][2, 1, 3])。
  • 若交换 p1p_1 与 p3p_3,得到数组 [3,2,1][3, 2, 1],其中包含 3 个是排列的子数组([1][1]、[2,1][2, 1]、[3,2,1][3, 2, 1])。
  • 若交换 p2p_2 与 p3p_3,得到数组 [1,3,2][1, 3, 2],其中包含 2 个是排列的子数组([1][1]、[1,3,2][1, 3, 2])。
  • 若将任意元素与其自身交换,得到数组 [1,2,3][1, 2, 3],其中包含 3 个是排列的子数组([1][1]、[1,2][1, 2]、[1,2,3][1, 2, 3])。

因此,最优的交换位置是第 22 位与第 33 位。

对于第三个样例,交换位置 22 与 55 上的元素后,所得数组为 [1,4,2,5,3][1, 4, 2, 5, 3]。其中唯一是排列的子数组仅有 [1][1] 和 [1,4,2,5,3][1, 4, 2, 5, 3]。可以证明该结果已达到最小值。

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

首页