排书
排书
此题使用 dfs
,然后判断条件里是类似于
这里比较显然,无需证明,比
考虑每次都会更改如上图所示的三个数的后继关系,所以每次最多消除
#include<bits/stdc++.h>
using namespace std;
const int N=15;
int n,q[N],w[5][N],depth;
int f(){
int cnt=2;
for(int i=0;i<n-1;++i)
cnt+=q[i+1]!=q[i]+1;
return cnt/3;
}
bool dfs(int u){
if(u+f()>depth)return 0;
if(!f())return 1;
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],q,sizeof q);
int x,y;
for(x=r+1,y=l;x<=k;++x,++y)q[y]=w[u][x];
for(x=l;x<=r;++x,++y)q[y]=w[u][x];
if(dfs(u+1))return 1;
memcpy(q,w[u],sizeof q);
}
}
return 0;
}
int main(){
int T;
cin>>T;
while(T--){
cin>>n;
for(int i=0;i<n;++i)cin>>q[i];
for(depth=0;depth<5&&!dfs(0);++depth);
if(depth>=5)puts("5 or more");
else printf("%d\n",depth);
}
return 0;
}
本文作者:wscqwq
本文链接:https://www.cnblogs.com/wscqwq/p/17396679.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步