1 //#define LOCAL 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<stack> 6 using namespace std; 7 const int maxn=1005; 8 stack<int>train; 9 int ss[maxn]; 10 int main() 11 { 12 #ifdef LOCAL 13 freopen("test.in","r",stdin); 14 #endif 15 int n,k; 16 while(scanf("%d",&n)&&n>0) 17 { 18 while(1) 19 { 20 scanf("%d",&ss[1]); 21 if(ss[1]==0)break; 22 for(int i=2;i<=n;i++) 23 scanf("%d",ss+i); 24 while(!train.empty()) 25 train.pop(); 26 k=1; 27 for(int i=1;i<=n;i++) 28 { 29 train.push(i); 30 while(!train.empty()&&train.top()==ss[k]) 31 { 32 k++; 33 train.pop(); 34 } 35 } 36 if(train.empty())printf("Yes\n"); 37 else printf("No\n"); 38 } 39 } 40 return 0; 41 }
题意: 一辆火车一次车厢依次进栈(1,2,3,4,5...,n),问有没有可能有安给出的方式出栈。
时间: 2024-10-14 20:07:28