栈问题
当初学数据结构的时候分析就分析不明白为什么有的序列不对
现在都要用代码来实现了
其实思路很清晰,对应好当前项就可以了
#include <cstdio>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int n, m, k;
vector<int> arr;
stack<int> brr;
int main(){
scanf("%d%d%d",&n,&m,&k);
arr.resize(m+1);
for(int i=0; i<k; i++){
for(int j=0; j<m; j++){
int a;
scanf("%d",&a);
arr[j] = a;
//用push—back没读进去?
}
int push = 1;
int x ;
bool flag = true;
while(brr.size() != 0) brr.pop();
for(int w=0; w<m; w++){
x = arr[w];
if(x > push){ //如果当前要弹出的值 比 轮到应该压入的值 大
//开始压入值,直到达到了要弹出的值为止
if(push > m){
flag = false;
break;
}
for(; push <= x; push++){
brr.push(push);
if(brr.size() > n){
flag = false;
break;
}
}
brr.pop();
}
else if(x == push){ //如果想等直接弹出
push++;
}
else if(x < push){ //如果小于,当前栈顶的值如果不是要弹出的说明错误
if(brr.size() !=0 && brr.top() == x){
brr.pop();
}
else{
flag = false;
break;
}
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}