Anton got bored during the hike and wanted to solve something. He asked Kirill if he had any new problems, and of course, Kirill had one.
You are given a permutation p of size n , and a number x that needs to be found. 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).
You decided that you are a cool programmer, so you will use an advanced algorithm for the search — binary search. However, you forgot that for binary search, the array must be sorted.
You did not give up and decided to apply this algorithm anyway, and in order to get the correct answer, you can perform the following operation no more than 2 times before running the algorithm: choose the indices i , j ( 1≤i,j≤n ) and swap the elements at positions i and j .
After that, the binary search is performed. At the beginning of the algorithm, two variables l=1 and r=n+1 are declared. Then the following loop is executed:
- If r−l=1 , end the loop
- m=⌊2r+l⌋
- If pm≤x , assign l=m , otherwise r=m .
The goal is to rearrange the numbers in the permutation before the algorithm so that after the algorithm is executed, pl is equal to x . It can be shown that 2 operations are always sufficient.
输入格式
Each test consists of multiple test cases. The first line contains a single integer t ( 1≤t≤2⋅104 ) — the number of test cases. Then follow the descriptions of the test cases.
The first line of each test case contains two integers n and x ( 1≤x≤n≤2⋅105 ) — the length of the permutation and the number to be found.
The second line contains the permutation p separated by spaces ( 1≤pi≤n ).
It is guaranteed that the sum of the values of n for all test cases does not exceed 2⋅105 .
输出格式
For each test case, output an integer k ( 0≤k≤2 ) on the first line — the number of operations performed by you. In the next k lines, output 2 integers i , j ( 1≤i,j≤n ) separated by a space, indicating that you are swapping the elements at positions i and j .
Note that you do not need to minimize the number of operations.
题意翻译
现给定一个长度为 n 的排列 p 以及一个数 x。
定义“在 p 中二分查找 x”为以下操作:
初始时有 l=1,r=n+1。
- 如果当前 r−l=1,退出二分查找,并且认定 pl 为二分查找的结果。
- 定义 m=⌊2l+r⌋。
- 如果 pm≤x,将 l 赋值为 m,否则将 r 赋值为 m。
不断重复以上三个操作直到得到结果。
显然当 p 未排序时二分查找的结果不一定为 x,现在你希望进行两次交换操作,使得操作后的排列 p 能使得二分查找的结果为 x。
一次交换操作为:选择 1≤i,j≤n,交换 pi,pj。
你需要给出交换的参数,容易证明两次操作总是足够的。
输入输出样例
输入 #1复制
5 6 3 1 2 3 4 5 6 6 5 3 1 6 5 2 4 5 1 3 5 4 2 1 6 3 4 3 1 5 2 6 3 2 3 2 1
输出 #1复制
0 1 3 4 2 2 4 1 5 2 4 5 2 4 1 1 3
#include<bits/stdc++.h>
#define int long long
#define N 1000005
#define INF 0x3f3f3f3f
using namespace std;
int a[N];
void solve( )
{
int n,x;
int index;
cin>>n>>x;
for(int i=1;i<=n;i++)
{cin>>a[i];
if(a[i]==x) index=i;//输入a[i]以后查找所寻找的x的位置
}
int l=1,r=n+1,mid;
while(l+1<r) //保证l与r不相邻
{
mid=(l+r)>>1;
if(a[mid]<=x) l=mid; //当中间值比被搜索值要小,左边界右移
else r=mid; //反之,右边界左移
}
if(a[l]==x) cout<<0<<endl; //当最后查询到的x应该在的位置就是x,那就不需要调整,直接输出0
else cout<<1<<endl<<l<<" "<<index<<endl; //否则,就要调换原本和最后查询应该在的位置
}
signed main()
{
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}