模拟一下出栈顺序就行。1~N顺序进栈,和序列匹配上就出栈。如果过程中栈大小大于给定大小就退出循环。循环结束栈为空就是yes。
#include <bits/stdc++.h>
using namespace std;
int main(){
int sz,range,num;
scanf("%d%d%d",&sz,&range,&num);
for(int i=0;i<num;i++){
int arr[range];
for(int j=0;j<range;j++)scanf("%d",&arr[j]);
stack<int> test;
int idx=0;
for(int j=1;j<=range;j++){
test.push(j);
if(test.size()>sz)break;
while(idx<range&&!test.empty()&&test.top()==arr[idx]){
test.pop();
idx++;
}
}
if(test.empty())printf("YES\n");
else printf("NO\n");
}
}