This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int > pp;
typedef long long ll;
int const N=3e5+10,oo=1e9,M=1e6+2;
ll const OO=1e18;
double const eps=1e-6,PI=acos(-1);
int mod=oo+7;
vector<int > v,o;
int n;
int solve(int idx){
if(idx==n)return 0;
if(v[idx]==o[idx])return solve(idx+1);
int mn=oo;
for(int j=idx+1;j<n;j++)if(v[j]==o[idx]){
reverse(v.begin()+j,v.end());
reverse(v.begin()+idx,v.end());
mn=min(mn,solve(idx+1)+1+(j!=n-1));
reverse(v.begin()+idx,v.end());
reverse(v.begin()+j,v.end());
}
return mn;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
cin>>n;
v.clear();
v.resize(n);
for(int i=0;i<n;i++)cin>>v[i];
o=v;
sort(o.begin(),o.end());
int an=solve(0)+1;
reverse(o.begin(),o.end());
an=min(an,solve(0));
cout<<an<<'\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |