310466: CF1838B. Minimize Permutation Subarrays

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Minimize Permutation Subarraystime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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

  • Select integers $i$, $j$, where $1 \le i, j \le n$, then
  • Swap $p_i$ and $p_j$.

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

Which choice of $i$ and $j$ will minimize the number of subarrays that are permutations?

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

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

Input

The first line of the input contains a single integer $t$ ($1 \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 $n$ ($3 \le n \le 2\cdot 10^5$) — the size of the permutation.

The next line of each test case contains $n$ integers $p_1, p_2, \ldots p_n$ ($1 \le p_i \le n$, all $p_i$ are distinct) — the elements of the permutation $p$.

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

Output

For each test case, output two integers $i$ and $j$ ($1 \le i, j \le n$)  — the indices to swap in $p$.

If there are multiple solutions, print any of them.

ExampleInput
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
Output
2 3
1 1
5 2
1 4
9 5
8 8
6 10
5 4
Note

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

  • If we swap $p_1$ and $p_2$, we get the array $[2, 1, 3]$, which has 3 subarrays that are permutations ($[1]$, $[2, 1]$, $[2, 1, 3]$).
  • If we swap $p_1$ and $p_3$, we get the array $[3, 2, 1]$, which has 3 subarrays that are permutations ($[1]$, $[2, 1]$, $[3, 2, 1]$).
  • If we swap $p_2$ and $p_3$, we get the array $[1, 3, 2]$, which has 2 subarrays that are permutations ($[1]$, $[1, 3, 2]$).
  • If we swap any element with itself, we get the array $[1, 2, 3]$, which has 3 subarrays that are permutations ($[1]$, $[1, 2]$, $[1, 2, 3]$).
So the best swap to make is positions $2$ and $3$.

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

Input

题意翻译

给定一个长度为 $n$ 的排列 $[p_1, p_2, ..., p_n]$ ,任选两个元素(可以相同)并交换**一次**,使得其所有子段中排列(长度不一定为 $n$ )的个数最少,输出被交换的元素的位置(下标从 $1$ 开始)。 对于样例 $1$ ,如果交换 $p_1$ 和 $p_2$ ,则可以得到排列 $ [2, 1, 3] $ ,含有 $3$ 个为排列的子段( $ [1] $ , $ [2, 1] $ , $ [2, 1, 3] $ ); 如果交换 $p_1$ 和 $p_3$ ,则可以得到排列 $ [3, 2, 1] $ ,含有 $3$ 个为排列的子段( $ [1] $ , $ [2, 1] $ , $ [3, 2, 1] $ ); 如果交换 $p_2$ 和 $p_3$ ,则可以得到排列 $ [1, 3, 2] $ ,含有 $2$ 个为排列的子段( $ [1] $ , $ [1, 3, 2] $ )。 故当交换 $p_2$ 和 $p_3$ 时最优。 对于样例 $3$ ,交换第 $2$ 和第 $5$ 个元素后,得到 $[1, 4, 2, 5, 3]$ ,仅包含两个为排列的子段( $[1]$ , $[1, 4,, 2, 5, 3]$ )。可以证明这是最优的情况。

Output

题目大意:给定一个长度为n的排列p,通过恰好一次交换p中的两个数,使得p的子排列数量最小。输出交换的两个位置i和j。

输入数据格式:第一行包含一个整数t,表示测试用例的数量。接下来每个测试用例包含两行,第一行是一个整数n,表示排列的长度。第二行包含n个整数,表示排列p的元素。

输出数据格式:对于每个测试用例,输出一行,包含两个整数i和j,表示交换的两个位置。如果有多个答案,输出其中任意一个。

举一个例子:

输入:
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

输出:
2 3
1 1
5 2
1 4
9 5
8 8
6 10
5 4题目大意:给定一个长度为n的排列p,通过恰好一次交换p中的两个数,使得p的子排列数量最小。输出交换的两个位置i和j。 输入数据格式:第一行包含一个整数t,表示测试用例的数量。接下来每个测试用例包含两行,第一行是一个整数n,表示排列的长度。第二行包含n个整数,表示排列p的元素。 输出数据格式:对于每个测试用例,输出一行,包含两个整数i和j,表示交换的两个位置。如果有多个答案,输出其中任意一个。 举一个例子: 输入: 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 输出: 2 3 1 1 5 2 1 4 9 5 8 8 6 10 5 4

加入题单

上一题 下一题 算法标签: