首页 > 其他 > 详细

LRJ-Example-06-02-Uva514

时间:2017-07-19 17:07:40      阅读:105      评论:0      收藏:0      [点我收藏+]
 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 }

 

LRJ-Example-06-02-Uva514

原文:http://www.cnblogs.com/patrickzhou/p/7206600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!