IDA* WA2&7 80分球条
查看原帖
IDA* WA2&7 80分球条
545026
syLph楼主2023/9/30 10:07
#include<cstdio>
#include<cstdlib>
#include<memory.h>
#include<algorithm>
using namespace std;
const int maxn = 100;
int n,p[maxn],mxd=1,found=0,q[maxn],f[maxn];
void reverse(int r){
    for(int i = 1 ; i <= r / 2 ; i ++) 
        swap(p[i],p[r-i+1]);
}
int estimate(){
    int ret = 0;
    for(int i = 1 ; i <= n ; i ++) ret += abs(p[i]-p[i+1])!=1;
    return ret;
}
void ida_star(int dep,int mxd){
    if(dep == mxd){
        if(!estimate())found = 1;
        return;
    }
    if(found==1) return;
    if(dep+estimate()>mxd) return;
    for(int i = 1 ; i <= n ; i ++) q[i] = p[i];
    for(int j = 2 ; j <= n ; j ++){
        reverse(j);
        ida_star(dep+1,mxd);
        for(int i = 1 ; i <= n ; i ++) p[i] = q[i];
    }
}
signed int main(){
    //freopen("testdata.in","r",stdin);
    scanf("%d",&n);
    for(int i = 1 ; i <= n ; i ++) scanf("%d",&p[i]),f[i] = p[i];
    sort(f+1,f+n+1);
    for(int i=1;i<=n;i++)p[i]=lower_bound(f+1,f+n+1,p[i])-f;
    p[n+1] = n + 1;
    while(1){
        ida_star(1,mxd);
        if(found) break;
        mxd+=1;
    }
    printf("%d\n",mxd);
    return 0;
}

#include #include #include<memory.h> #include using namespace std; const int maxn = 100; int n,p[maxn],mxd=1,found=0,q[maxn],f[maxn]; void reverse(int r){ for(int i = 1 ; i <= r / 2 ; i ++) swap(p[i],p[r-i+1]); } int estimate(){ int ret = 0; for(int i = 1 ; i <= n ; i ++) ret += abs(p[i]-p[i+1])!=1; return ret; } void ida_star(int dep,int mxd){ if(dep == mxd){ if(!estimate())found = 1; return; } if(found==1) return; if(dep+estimate()>mxd) return; for(int i = 1 ; i <= n ; i ++) q[i] = p[i]; for(int j = 2 ; j <= n ; j ++){ reverse(j); ida_star(dep+1,mxd); for(int i = 1 ; i <= n ; i ++) p[i] = q[i]; } } signed int main(){ //freopen("testdata.in","r",stdin); scanf("%d",&n); for(int i = 1 ; i <= n ; i ++) scanf("%d",&p[i]),f[i] = p[i]; sort(f+1,f+n+1); for(int i=1;i<=n;i++)p[i]=lower_bound(f+1,f+n+1,p[i])-f; p[n+1] = n + 1; while(1){ ida_star(1,mxd); if(found) break; mxd+=1; } printf("%d\n",mxd); return 0; }

2023/9/30 10:07
加载中...