题意
给一个长度为n的数组,每两个数字下标只差大于等于x都能交换,问最后能不能变成非递减序列?
解析
对于长度大于等于
2
∗
x
2*x
2∗x的序列,其每个数字都能互相交换到达所要到的位置。
对于长度小于
2
∗
x
2*x
2∗x的序列,对于区间
[
1
,
n
−
x
]
[1,n-x]
[1,n−x]可以和区间
[
x
+
1
,
n
]
[x+1,n]
[x+1,n]互相交换,因此如果这种序列想要有序,必须保证
[
n
−
x
,
x
+
1
]
[n-x,x+1]
[n−x,x+1]这段区间里的数字已经是最终排完序的数字。
代码
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e5+5;
int r[N],r2[N];
int main() {
ios::sync_with_stdio(0),cin.tie(0);
int t ;
cin>>t;
while(t--){
int n,x;
cin>>n>>x;
for(int i=1;i<=n;i++){
cin>>r[i];
r2[i]=r[i];
}
sort(r2+1,r2+1+n);
if(n>=2*x){
puts("YES");
}
else{
int L=n-x+1,R=x;
bool f=1;
for(int i=L;i<=R;i++){
if(r[i]!=r2[i]){
f=0;
break;
}
}
if(f)puts("YES");
else puts("NO");
}
}
return 0;
}