time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
Hemose was shopping with his friends Samez, AhmedZ, AshrafEzz, TheSawan and O_E in Germany. As you know, Hemose and his friends are problem solvers, so they are very clever. Therefore, they will go to all discount markets in Germany.
Hemose has an array of nn integers. He wants Samez to sort the array in the non-decreasing order. Since it would be a too easy problem for Samez, Hemose allows Samez to use only the following operation:
-
Choose indices ii and jj such that 1≤i,j≤n1≤i,j≤n, and |i−j|≥x|i−j|≥x. Then, swap elements aiai and ajaj.
Can you tell Samez if there's a way to sort the array in the non-decreasing order by using the operation written above some finite number of times (possibly 00)?
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤105)(1≤t≤105). Description of the test cases follows.
The first line of each test case contains two integers nn and xx (1≤x≤n≤105)(1≤x≤n≤105).
The second line of each test case contains nn integers a1,a2,...,ana1,a2,...,an (1≤ai≤109)(1≤ai≤109).
It is guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052⋅105.
Output
For each test case, you should output a single string.
If Samez can sort the array in non-decreasing order using the operation written above, output "YES" (without quotes). Otherwise, output "NO" (without quotes).
You can print each letter of "YES" and "NO" in any case (upper or lower).
Example
input
Copy
4 3 3 3 2 1 4 3 1 2 3 4 5 2 5 1 2 3 4 5 4 1 2 3 4 4
output
Copy
NO YES YES YES
Note
In the first test case, you can't do any operations.
In the second test case, the array is already sorted.
In the third test case, you can do the operations as follows:
- [5,1,2,3,4][5,1,2,3,4], swap(a1,a3)swap(a1,a3)
- [2,1,5,3,4][2,1,5,3,4], swap(a2,a5)swap(a2,a5)
- [2,4,5,3,1][2,4,5,3,1], swap(a2,a4)swap(a2,a4)
- [2,3,5,4,1][2,3,5,4,1], swap(a1,a5)swap(a1,a5)
- [1,3,5,4,2][1,3,5,4,2], swap(a2,a5)swap(a2,a5)
- [1,2,5,4,3][1,2,5,4,3], swap(a3,a5)swap(a3,a5)
- [1,2,3,4,5][1,2,3,4,5]
(Here swap(ai,aj)swap(ai,aj) refers to swapping elements at positions ii, jj).
题目大意:给你一个n和一个k,接下来给出n个数,你可以对其进行以下操作:
选取一个ai和一个aj,前提条件是满足|i-j|>=x,将2者的数值进行交换
问能不能通过这种操作将这个数列变成一个不下降的数列,如果可以就输出YES,否则输出NO
分析:如果x越小,就越有可能得到目的序列,当n>=2*x的时候每个数字都可以进行交换操作,因此就能通过若干次变换得到目的数列,接下来再来考虑x比较大的时候,x比较大的话那势必会有一部分数列不能进行交换操作,那我们只要判断一下该部分的数列[n-x+1,x]是不是和目的数列相吻合即可。
上代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int a[N],b[N];
int n,x;
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>x;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
if(n>=2*x)
cout<<"YES"<<endl;
else
{
sort(b+1,b+1+n);
int flag=0;
for(int i=n-x+1;i<=x;i++)
{
if(a[i]!=b[i])
{
flag=1;
break;
}
}
if(!flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
return 0;
}