火车进站
1 #include <iostream> 2 #include <stack> 3 #include <stdio.h> 4 using namespace std; 5 const int MAXN = 1000 + 10; 6 7 int n, target[MAXN]; 8 9 int main(){ 10 while(cin >> n){ 11 stack<int> s; 12 int A = 1, B = 1; 13 for(int i = 1; i <= n; i++){ 14 cin >> target[i]; 15 } 16 int ok = 1; 17 while(B <= n){ 18 if(A == target[B]){ 19 A++; 20 B++; 21 } 22 else if(!s.empty() && s.top() == target[B]){ 23 s.pop(); 24 B++; 25 } 26 else if(A < n){ 27 s.push(A++); 28 } 29 else{ 30 ok = 0; 31 break; 32 } 33 } 34 printf("%s\n",ok ? "Yes" : "No"); 35 } 36 return 0; 37 }