Uva514

https://vjudge.net/problem/UVA-514

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN=1010;
 4 int target[MAXN];
 5 int main()
 6 {
 7     int n,f;
 8     while(scanf("%d",&n)&&n)
 9     {
10         while(1){
11         f=1;
12         stack<int>s;
13         int A=1,B=1;
14         for(int i=1;i<=n;i++)
15          {
16          cin>>target[i];
17          if(target[1]==0)
18          {
19              cout<<endl;
20              f=0;
21              break;
22          }
23          }
24          if(f==0)break;
25         int ok=1;
26         while(B<=n)
27         {
28             if(target[B]==A)
29                 A++,B++;
30             else if(!s.empty()&&s.top()==target[B])
31             {
32                 s.pop();
33                 B++;
34             }
35             else if(A<=n)
36                 s.push(A++);
37             else
38             {
39                 ok=0;
40                 break;
41             }
42         }
43         printf("%s\n",ok?"Yes":"No");
44         }
45     }
46     return 0;
47 }

 

1)栈的应用

用A来代表车厢序号,B代表target数组用来表示列车出站的序号顺序。B用来表示已经驶进B的车辆数。用stack<int>s表示C。

1.A中首元素==B首元素,A直接驶入B;

2.A中首元素!=B首元素,栈首元素==B首元素,栈中首元素出栈;

3.A中首元素!=B首元素,栈首元素!=B首元素,A中元素个数>0,A中元素进入栈;

4.A中首元素!=B首元素,栈首元素!=B首元素,A中元素个数<=0,该解不成立。

2)输入输出的形式奇特,需要多加注意。

 

转载于:https://www.cnblogs.com/zuiaimiusi/p/10667226.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值