uva514Rails(栈)
题目链接:
lrj--p140。
1 #include<cstdio> 2 #include<stack> 3 using namespace std; 4 int main() 5 { 6 int n; 7 int a[1010]; 8 stack<int> b; 9 while(scanf("%d",&n)!=EOF&&n) 10 { 11 12 while( scanf("%d",&a[1])&&a[1]){ 13 int ok=1; 14 for(int i=2;i<=n;i++) 15 scanf("%d",&a[i]); 16 17 while(!b.empty()) b.pop(); 18 int p=1,q=1; 19 while(q<=n) 20 { 21 if(a[q]==p) {p++;q++;} 22 else if(!b.empty()&&b.top()==a[q]) {b.pop();q++;} 23 else if(p<=n) {b.push(p);p++;} 24 else{ ok=0;break;} 25 } 26 printf("%s\n",ok?"Yes":"No"); 27 } 28 printf("\n"); 29 } 30 }