http://tjuacm.chaosheng.top/problem.php?id=1250
利用栈,判断是否符合条件。
参考
https://blog.csdn.net/Walton_/article/details/53150777?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
#include <iostream> #include <stack> using namespace std; const int MAXN = 1001; int num[MAXN]; int main(){ int n; while(cin >> n){ if(n == 0) break; while(cin >> num[1]){ if(num[1] == 0){ printf("\n"); break; } for(int i = 2; i <= n; i++){ cin >> num[i]; } stack<int> s; int a = 1; int b = 1; bool flag = true; while(b <= n){ if(a == num[b]){ a++; b++; }else if(!s.empty() && s.top() == num[b]){ s.pop(); b++; }else if(a <= n){ s.push(a); a++; }else{ flag = false; break; } } if(flag){ printf("Yes\n"); }else{ printf("No\n"); } } } return 0; }