PAT1051. Pop Sequence (25)

#include <iostream>
#include <stack>
using namespace std;
int sizeMax,n,k;
stack<int> sta;
int queueNum;
int num;
int j;int flag=0;
int main(){
	cin>>sizeMax>>n>>k;
	while(k--){
		while(!sta.empty()){sta.pop();}
		queueNum=1;
		flag=0;
		for(int i=0;i<n;i++){
			cin>>num;
			if(flag){continue;}
			if(num>queueNum){
				for(j=queueNum;j<num;j++){
					sta.push(j);
				}
				if(sta.size()>=sizeMax){
					flag=1;
					continue;
				}
				queueNum=num+1;
			}else if(num<queueNum){
				int top=sta.top();
				if(top!=num){
					flag=1;
					continue;
				}
				sta.pop();
			}else{
				queueNum++;
			}
		}
		if(flag){
			cout<<"NO"<<endl;
		}else{
			cout<<"YES"<<endl;
		}
	}
	return 0;
}

 

posted @ 2016-03-27 18:53  Yellowman  阅读(119)  评论(0编辑  收藏  举报
TVRBMExqRXlPQzR5TXpjdU1UVTEK\n