题目链接:https://pintia.cn/problem-sets/988034414048743424/problems/988038770777137152
解题思路:将1到n模拟进栈,当当前栈顶与当前给定的待出栈的数字相同时,则出栈,一直模拟,如果将n个数全部模拟进栈后,最后堆栈为空,那么YES,否则NO。同时要注意模拟过程中,判断堆栈容量不能超过m
题解:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<queue>
#include<stack>
using namespace std;
const int maxn = 1e5 + 10;
#define ll long long
int m, n, k;
int a[1100][1100];
int main() {
cin >> m >> n >> k;
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
}
}
for (int i = 1; i <= k; i++) {
stack<int> s;
int cur = 1;
int fa = 0;
for (int j = 1; j <= n; j++) {
if (j == a[i][cur]) {
s.push(j);
if (s.size() > m) {
fa = 1;
break;
}
cur++;
s.pop();
while (!s.empty() && a[i][cur] == s.top()) {
s.pop();
cur++;
}
}
else {
s.push(j);
}
if (s.size() > m) {
fa = 1;
break;
}
}
if (s.empty())
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}