You are given a permutation p of size n. You want to minimize the number of subarrays of p�hat are permutations. In order to do so, you must perform the following operation exactly once:
- Select integers i, j, where 1≤i,j≤n, then
- Swap pi and pj.
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][5,4,1,2,3]. If instead we choose i=j=5, the resulting array will be [5,1,4,2,3][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 (22 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 44 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≤t≤104) — 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≤n≤2⋅105) — the size of the permutation.
The next line of each test case contains n integers p1,p2,…pn (1≤pi≤n, all pi are distinct) — the elements of the permutation p.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output two integers i and j (1≤i,j≤n) — the indices to swap in p.
If there are multiple solutions, print any of them.
Example
input
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 p1 and p2, 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 p1 and p3, 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 p2 and p3, 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.
题意:
给出一个长度为n的排列a,下标为1~n,现在你可以对其中两个位置的数进行交换(且只能交换一次),从而得到b数组,问怎样才可以使b中的所有连续子序列成为排列的个数最少,输出交换的位置。
思路:
我们可以发现,不管怎么交换,我们总会有长度为1的排列和长度为n的排列。
倘若我们要得到长度大于1的排列(假设需要得到长度为k的排列),那么1和2必须得在里面,而且k+1这个数必须不在里面。
所以我们可以直接将n这个数放到1和2之间,这样便是最优情况。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a[200001];
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
a[x]=i;
}
if(a[n]>a[1]&&a[n]>a[2])
cout<<a[n]<<" "<<max(a[1],a[2])<<endl;
else if(a[n]<a[1]&&a[n]<a[2])
cout<<a[n]<<" "<<min(a[1],a[2])<<endl;
else
cout<<1<<" "<<1<<endl;
}
return 0;
}