pat 1051
来源:互联网 发布:闲鱼淘宝介入卖家赢 编辑:程序博客网 时间:2024/04/30 18:59
#include<stdio.h>#include<stdlib.h>#include<stack>using namespace std;int in[1005];int out[1005][1005];int m,n,k;stack<int> st;int main(){ scanf("%d%d%d",&m,&n,&k); for(int i=0;i<n;i++) in[i]=i+1; for(int i=0;i<k;i++) for(int j=0;j<n;j++) scanf("%d",&out[i][j]); for(int p=0;p<k;p++){ int i=0,j=0,st_num=0; bool flag=true; while(!st.empty()) st.pop();//st忘了清空了 while(i<n&&j<n){ if(in[i]==out[p][j]){i++;j++;} else if(in[i]<out[p][j]){ st_num+=(out[p][j]-in[i]+1); if(st_num>m){flag=false;break;} for(int q=in[i];q<=out[p][j];q++) st.push(q); i+=(out[p][j]-in[i]+1); } else{ if(st.top()==out[p][j]){ st.pop(); st_num--; j++; }else{ flag=false; break; } } } if(flag){ while(!st.empty()){ if(st.top()==out[p][j]){st.pop();j++;} else{ flag=false;break;} } } if(flag) printf("YES\n"); else printf("NO\n"); } //system("pause"); return 0;}