首页 > 代码库 > LRJ-Example-06-02-Uva514
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 }
LRJ-Example-06-02-Uva514
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。