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 }
时间: 2024-10-12 20:44:31