cjweffort

博客园 首页 联系 订阅 管理

// 1051. Pop Sequence.cpp: 主项目文件。

#include "stdafx.h"
#include <cstdio>
#include <stack>
using namespace std;

stack<int> S;
int stackMaxSize,arrMaxSize;

void readRemain(int rem){
	int temp;
	for(int i=0;i<rem;i++)
		scanf("%d",&temp);
}

bool isRightPopSequence(int &ccur){
	while(!S.empty())
		S.pop();
	int curPos=1;
	for(int i=0;i<arrMaxSize;i++){
		ccur=i;
		int temp;
		scanf("%d",&temp);
		if(S.empty()){
			while(curPos<=arrMaxSize&&curPos!=temp){
				S.push(curPos++);
				if(S.size()>stackMaxSize-1){
					return false;
				}
			}
			if(curPos==arrMaxSize+1){
				return false;
			}
			curPos++;
		}
		else{
			int topValue=S.top();
			if(topValue==temp)
				S.pop();
			else{
				while(curPos<=arrMaxSize&&curPos!=temp){
					S.push(curPos++);
					if(S.size()>stackMaxSize-1){
						return false;
					}
				}
				if(curPos==arrMaxSize+1){
					return false;
				}
				curPos++;
			}
		}
	}
	return true;
}

int main()
{
	int n;
	scanf("%d%d%d",&stackMaxSize,&arrMaxSize,&n);
	while(n--){
		int ccur=-1;
		bool tag=isRightPopSequence(ccur);
		readRemain(arrMaxSize-ccur-1);
		if(tag)
			puts("YES");
		else
			puts("NO");
	}
	return 0;
}

考前再刷题,攒点rp,明天机试要发挥好啊奋斗


posted on 2013-03-24 20:01  cjweffort  阅读(184)  评论(0)    收藏  举报