sicily/1509. Rails

 1  #include  <iostream>
 2  #include  <stack>
 3  using  namespace std;
 4  int train[ 1001];
 5  int main()
 6 {
 7          int t;
 8          int fs= 0;
 9          while(cin>>t&&t!= 0)
10         {
11                  while(cin>>train[ 0]&&train[ 0]!= 0)
12                 {
13                          for( int i= 1;i<t;i++)
14                         {
15                                 cin>>train[i];
16                         }
17                         stack< int> s;
18                          int k= 0;
19                          for( int i= 1;i<=t;i++)
20                         {
21                                  //从A到来的车与目标序列相同,让A车过
22                                  if(i==train[k])
23                                         k++;
24                                  else
25                                 {
26                                          //如果栈中的车与目标队列相同,那么就是让队列中的车出来
27                                          while(!s.empty()&&s.top()==train[k])
28                                         {
29                                                 s.pop();
30                                                 k++;
31                                         }
32                                         
33                                         s.push(i);
34                                 }
35                         }
36                          //最后清空堆栈
37                          while(!s.empty())
38                         {
39                                  if(s.top()==train[k])
40                                         k++;
41                                 s.pop();
42                         }
43                          if(k==t)
44                                 cout<< "Yes"<<endl;
45                          else
46                                 cout<< "No"<<endl;
47                         
48                 }
49                 cout<<endl;
50         }
51 }

转载于:https://www.cnblogs.com/congzc/archive/2011/07/12/2329940.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值