题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=259
题意:栈的应用,算法竞赛入门经典里的原题
Sample Input
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0
Sample Output
Yes
No
Yes
#include<stdio.h>
#include<stack>
#include<iostream>
using namespace std;
int main(){
int n,tar[10100],ok=1;
stack<int> sta;
while(cin >> n &&n){
while(cin >> tar[1]){
if(tar[1]==0) {printf("\n");break;}//这里PE了3次
for(int i=2;i<=n;i++)
cin >> tar[i];
int jud=1,A=1,B=1;
//下面代码和入门经典的相似
while(B<=n){
if(A==tar[B]) {A++;B++;}
else if(!sta.empty() && sta.top()==tar[B]) {B++;sta.pop();}
else if(A<=n) sta.push(A++);
else {jud=0;break;}
}
if(jud) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}