弹出序列
https://www.acwing.com/problem/content/1537/
思路:
用一个栈来模拟整个过程,注意什么时候应该pop,如果判断结果。
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
const int N = 1010;
int m, n, k;
int a[N];
bool check()
{
stack<int> stk;
for (int i = 1, j = 0; i <= n; i++)
{
stk.push(i);
if (stk.size() > m) return false;
while (stk.size() && stk.top() == a[j])
{
stk.pop();
j++;
}
}
return stk.empty();
}
int main()
{
scanf("%d%d%d", &m, &n, &k);
while (k--)
{
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
if (check()) puts("YES");
else puts("NO");
}
return 0;
}