您的位置:首页 > 大数据 > 人工智能

UVA514 Rails(铁轨)

2016-12-25 22:20 441 查看
#include<cstdio>

#include<stack>

using namespace std;

const int maxn=1010;

int a[maxn];

int main(){
int n;
while(scanf("%d",&n)==1 && n!=0){
while(scanf("%d",&a[1])==1 && a[1]!=0){
for (int i=2;i<=n;i++){
scanf("%d",&a[i]);
}
stack<int>s;
int A=1,B=1;
int ok=1;
while(B<=n){
if(A==a[B]){
A++;B++;
}
else if(!s.empty() && s.top()==a[B]){
B++;s.pop();
}
else if(A<=n){
s.push(A++);
}
else {
ok=0;
break;
}
}
if(ok)printf("Yes\n");
else printf("No\n");
}
printf("\n");
}
return 0;
}

//需要注意的是车厢可以看成不连续的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: