LRJ-Example-06-02-Uva514

 1 #define _CRT_SECURE_NO_WARNINGS
 2 
 3 #include<cstdio>
 4 #include<stack>
 5 using namespace std;
 6 const int MAXN = 1000 + 10;
 7 
 8 int n, target[MAXN];
 9 
10 int main() {
11     // scanf() returns the number of input items matched and assigned
12     while (scanf("%d", &n) == 1 && n != 0) {
13         while (true) {
14             // the last line of the block contains just 0
15             scanf("%d", &target[1]);
16             if (target[1] == 0) { printf("\n"); break; }
17 
18             for (int i = 2; i <= n; i++)
19                 scanf("%d", &target[i]);
20 
21             stack<int> s;
22             // assume an input array input[] containing 1, 2, ..., n in sequence, input[i] == i
23             // A is the index of input[], and B is the index of target[]
24             int A = 1, B = 1;
25             bool ok = true;
26             while (B <= n) {
27                 if (A == target[B]){
28                     A++;
29                     B++;
30                 } else if (!s.empty() && s.top() == target[B]){
31                     s.pop();
32                     B++;
33                 } else if (A <= n){
34                     s.push(A++);
35                 } else {
36                     ok = false;
37                     break;
38                 }
39             }
40 
41             printf("%s\n", ok ? "Yes" : "No");
42         }
43     }
44     return 0;
45 }

 

转载于:https://www.cnblogs.com/patrickzhou/p/7206600.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值