- 排序游戏(sort)
TLE on #3!!!
- 2023-3-18 10:43:37 @
#include<bits/stdc++.h>
using namespace std;
int r(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int n=r(),k=r(),a,t2[101022];string s,s2,s3,t1[101022];map<string,bool>sb;
int main(){
s=s2=" ";
for(int i=1;i<=n;++i){
a=r();s+=(a+'0');s2+=(i+'0');
}if(s==s2){cout<<0;return 0;}
int x=1,y=1,nn=k/2;t1[x]=s;t2[x]=0;sb[s]=1;
while(x<=y){
s3=t1[x++];
for(int i=1;i<=n-k+1;++i){
s=s3;
for(int j=1;j<=nn;++j)swap(s[i+j-1],s[i+k-j]);
if(s==s2){
cout<<t2[x-1]+1;return 0;
}
if(sb[s]!=1){
t1[++y]=s;t2[y]=t2[x-1]+1;
sb[s]=1;
}
}
}
cout<<-1;
}
1 条评论
-
应哲宇 LV 6 @ 2023-3-18 10:51:55
把快读改成cin就过了
- 1
信息
- ID
- 318
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- (无)
- 递交数
- 65
- 已通过
- 24
- 上传者