求条
查看原帖
求条
1246763
ny_WYR楼主2024/11/30 16:19

rt
判题结果 如你所见,全部RE
代码:

#include<bits/stdc++.h>
using namespace std;
int const N=20;
int a[N],b[N],c[N];
int n;
int f(){
	int res=0;
	for(int i=1;i<=n;i++){
		if(abs(a[i+1]-a[i])!=1) res++;
	}
	return res;
}
bool dfs(int u,int max_depth,int pre){
	int ff=f();
	int w[N];
	if(u+ff>max_depth) return false;
	if(ff==0){
		return true;
	}
	for(int i=1;i<=n;i++){
		if(i==pre) continue;
		memcpy(w,a,sizeof(a));
		for(int j=1;j<=(i+1)>>1;j++){
			swap(a[j],a[i-j+1]);
		}
		if(dfs(u+1,max_depth,i)) return true;
		memcpy(a,w,sizeof(w));
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	memcpy(b,a,sizeof(a));
	sort(b+1,b+1+n);
	int m=unique(b+1,b+1+n)-b-1;
	for(int i=1;i<=n;i++){
		c[i]=lower_bound(b+1,b+1+m,a[i])-b;
	}
	memcpy(a,c,sizeof(c));
	a[n+1]=n+1;
	int depth=0;
	for(;!dfs(0,depth,0);depth++);
	printf("%d",depth);
	return 0;
}
2024/11/30 16:19
加载中...