1051 Pop Sequence (25分)
简答的栈模拟题,只要把过程想清楚就能做出来。
扫描到某个元素时候,假如比栈顶元素还大,说明包括其本身的在内的数字都应该入栈。将栈顶元素和序列比对即可,相同则弹栈,继续扫描;否则无法生成满足条件的序列。注意栈满时不能入栈
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e3+100; int m, n, q, a[maxn], sta[maxn]; bool flag; int main(){ scanf("%d%d%d", &m, &n, &q); while(q--){ for(int i = 1; i <= n; i++) scanf("%d", &a[i]); int k = 1, max_num = 0, t = 0; flag = true; while(k<=n){ while(max_num<a[k]&&t<m) sta[++t] = ++max_num; if(sta[t]==a[k]) t--, k++; else{ flag = false; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } }
原文:https://www.cnblogs.com/wizarderror/p/14289072.html