太苦逼的一道题,关于输入,关于栈的初始定义。
不过这道题应该学会的是分部模拟。
车厢进栈有两种情况,一种是进直接出。另一种是进了不出。接着就是判断A,B就行。
#include<cstdio>
#include<stack>
using namespace std;
const int MAXN = 1010;
int n,target[MAXN];
int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d",&n) , n){
while(scanf("%d",&target[1]) , target[1]){ //是0就跳过
stack<int >s; //初始条件不变
for(int i = 2;i <= n; i++)
scanf("%d",&target[i]);
int A = 1,B = 1;
int ok = 1;
while(B <= n){
if(A == target[B]){ A++;B++;}
else if(!s.empty() && s.top() == target[B]) {s.pop(); B++;}
else if(A <= n) s.push(A++);
else { ok = 0; break;}
}
printf("%s\n",ok ? "Yes" : "No");
}
printf("\n");
}
return 0;
}