思路:因为arr的大小才500所以采取硬暴力的方法,由于需要输出一个非递减的排列,又因为换的元素比arr[i]本身小,所以必须从前向后换,但是不确定是否前面元素已经排好序,所以此时枚举arr中的所有元素然后找最小值即可,还可以进行减枝,但是没有必要
#include<bits/stdc++.h>
using namespace std;
const int N = 505;
vector<int>arr,store;
int dp[N];
int n;
bool check(){
for(int i =1;i<=n;i++){
if(arr[i]<arr[i-1])
return false;
}
return true;
}
void solve(){
int x;cin>>n>>x;
int min_=1e9;
store.resize(N);
for(int i =1;i<=n;i++)cin>>store[i];
for(int i =1;i<=n;i++){
int cnt =0;
arr = store;
if(check()){
min_=0;
break;
}
for(int j =i;j<=n;j++){
if(check())
{
min_ = min(min_,cnt);
break;
}
else{
if(arr[j]>x)
{
swap(arr[j], x);
cnt++;
}
}
}
}
if(min_==1e9)
cout<<-1<<endl;
else
cout<<min_<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int __=1;
cin>>__;
while (__--) {
solve();
}
}