背景:1A
思路:栈模拟
我的代码:
#include <set> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <map> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #define LL long long int using namespace std; const int M=1009,INF=0x3fffffff; int n,train[M]; int main(void){ log:while(scanf("%d",&n),n){ while(true){ stack<int> s1,s2; bool ans=false; for(int i=1;i <= n;i++){ scanf("%d",&train[i]); if(!train[i]){ printf("\n"); goto log; } s1.push(n-i+1); } for(int i=1;;i++){ if(i > n){ans=true;break;} if(!s2.empty() && s2.top() == train[i]){ s2.pop(); }else if(!s1.empty() && train[i] >= s1.top()){ while(true){ if(!s1.empty() && s1.top() == train[i]){ s1.pop(); break; } int temp=s1.top(); s1.pop(); s2.push(temp); } }else break; } if(ans) printf("Yes\n"); else printf("No\n"); } } return 0; }
原文地址:http://blog.csdn.net/jibancanyang/article/details/44923859