Thinking: It can be abstracted as the shortest path problem and done with IDA*. We need an evaluation function, and the evaluation function should not be greater than the actual number of steps. We found that every time a modification is made, the successor of 3 places will be changed. When in order, each number and its successor satisfy a[i+1]=a[i]+1, so we can get this The sequence modification to order requires several modifications, then we regard it as an evaluation function, f () = ⌈ tot 3 ⌉ f()=\lceil \frac{tot}3\rceilf()=⌈3t o t⌉ , we go to enumerate the number of his modification and iteratively deepen. When the number of steps calculated by the evaluation function and the number of steps already taken are greater than the maximum number of steps that can be taken, there is no solution, and the evaluation function is 0 to indicate that there is a solution.
#pragma GCC optimize(2)#include<bits/stdc++.h>usingnamespace std;typedeflonglong ll;#define SIS std::ios::sync_with_stdio(false)#define space putchar(' ')#define enter putchar('\n')#define lson root<<1#define rson root<<1|1typedef pair<int,int> PII;typedef pair<int,PII> PIII;constint mod=1e9+7;constint N=2e5+5;constint inf=0x7f7f7f7f;intgcd(int a,int b){
return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){
return a*(b/gcd(a,b));}template<classT>voidread(T &x){
char c;bool op =0;while(c =getchar(), c <'0'|| c >'9')if(c =='-')
op =1;
x = c -'0';while(c =getchar(), c >='0'&& c <='9')
x = x *10+ c -'0';if(op)
x =-x;}template<classT>voidwrite(T x){
if(x <0)
x =-x,putchar('-');if(x >=10)write(x /10);putchar('0'+ x %10);}
ll qsm(int a,int b,int p){
ll res=1%p;while(b){
if(b&1)
res=res*a%p;
a=1ll*a*a%p;
b>>=1;}return res;}int n;int a[25];int w[5][25];intf(){
int tot=0;for(int i=0;i+1<n;i++){
if(a[i+1]!=a[i]+1)tot++;}//cout<<(tot+2)/3<<endl;return(tot+2)/3;}booldfs(int u,int mdep){
if(u+f()>mdep)returnfalse;if(f()==0)returntrue;for(int len=1;len<=n;len++){
for(int l=0;l+len-1<n;l++){
int r=l+len-1;for(int k=r+1;k<n;k++){
memcpy(w[u],a,sizeof a);int y=l;for(int x=r+1;x<=k;x++,y++) a[y]=w[u][x];for(int x=l;x<=r;x++,y++) a[y]=w[u][x];if(dfs(u+1,mdep))returntrue;memcpy(a,w[u],sizeof a);}}}returnfalse;}intmain(){
int t;
cin>>t;while(t--){
cin>>n;for(int i=0;i<n;i++){
cin>>a[i];}int dep=0;while(dep<5&&!dfs(0,dep))dep++;if(dep>=5)cout<<"5 or more"<<endl;else cout<<dep<<endl;}return0;}