Exercise the usage of the stack and pay attention to the input format. I have not understood it for a long time.
1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <stack> 6 using namespace std; 7 8 const int maxn = 1000 + 10; 9 int n, target[maxn];10 11 int main(void)12 {13 #ifdef LOCAL14 freopen("514in.txt", "r", stdin);15 #endif16 17 while(scanf("%d", &n) == 1 && n)18 {19 while(scanf("%d", &target[1]) == 1 && target[1])20 {21 for(int i = 2; i <= n; ++i) scanf("%d", &target[i]);22 stack<int> s;23 int A = 1, B = 1;24 int ok = 1;25 while(B <= n)26 {27 if(A == target[B]) { A++; B++; }28 else if(!s.empty() && s.top() == target[B]) { s.pop(); B++; }29 else if(A <= n) s.push(A++);30 else { ok = 0; break; }31 }32 printf("%s\n", ok ? "Yes" : "No");33 }34 printf("\n");35 }36 37 return 0;38 }
Code Jun
Ultraviolet (use of stack) 514 rails