02-线性结构4. Pop Sequence (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.

Output Specification:

For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.

Sample Input:

5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

Sample Output:

YES
NO
NO
YES
NO

提交代码

 #include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
int line[];
int main(){
//freopen("D:\\input.txt","r",stdin);
int m,n,k;
int i,j,num,hav,count;
scanf("%d %d %d",&m,&n,&k);
while(k--){
stack<int> s;
count=;
hav=;
for(i=;i<n;i++)
scanf("%d",&line[i]);
for(i=;i<n;i++){
num=line[i];
//hav表示的是入栈过的最大数
//当前栈顶元素<=hav,凡是<=hav的元素都已经进入到栈中
//1.num>hav时,num肯定>当前的栈顶元素,故hav+1到num的元素要进栈&&栈的最大元素个数<=m
//2.不满足1时,num<hav&&num<=当前栈顶元素,不断退栈,直到退到满足条件的元素或者栈空
//3.
if(hav<num&&num-hav+count<=m){//num>hav时,num肯定>当前的栈顶元素,故hav+1到num的元素要进栈&&栈的最大元素个数<=m
for(j=hav+;j<num;j++){ //cout<<"j: "<<j<<endl; s.push(j);
count++;
}
hav=num;
}
else if(!s.empty()&&s.top()>=num){
//不满足1时,num<hav&&num<=当前栈顶元素,不断退栈,直到退到满足条件的元素或者栈空
while(!s.empty()&&s.top()!=num){
s.pop();
count--;
}
if(s.empty()){//栈空说明这个元素之前已经pop过,不符合题意
break;
}
else{//找到满足的元素
s.pop();
count--;
}
}else{//其他情况不符合题意
break;
}
}
if(i==n){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return ;
}

pat02-线性结构4. Pop Sequence (25)的更多相关文章

  1. 线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...

  2. PTA 02-线性结构4 Pop Sequence (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/665 5-3 Pop Sequence   (25分) Given a stack wh ...

  3. 数据结构练习 02-线性结构3. Pop Sequence (25)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  4. 02-线性结构4 Pop Sequence (25 分)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  5. 02-线性结构4 Pop Sequence

    02-线性结构4 Pop Sequence   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...

  6. PAT 解题报告 1051. Pop Sequence (25)

    1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...

  7. PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

    1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ord ...

  8. PAT 1051 Pop Sequence (25 分)

    返回 1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ...

  9. 02-线性结构4 Pop Sequence (25 分)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

随机推荐

  1. 合并datagridview 条件合并行数据

    public void HeBing() { int rowsCount; int CellCount; rowsCount = FG1.Rows.Count; CellCount = FG1.Col ...

  2. 多个fragment中重叠问题的解决方法

    这个方法适用性有限. 我的是一个mainActivity,然后下部四个按钮,点击时先隐藏所有的fragment,然后再new一个新的出来,如果存在,则直接显示出来,看上去一切都没有问题. 但是通过fr ...

  3. ERC230 VS ERC223

    ERC223对ERC220的改进 ERC223是以太坊上最新的代币(token)接口标准,主要是为了解决ERC220代币转账丢失问题,那么怎么解决的呢,一起来看看. 1. ERC220 存在问题 ER ...

  4. 洛谷P3355 骑士共存问题(最小割)

    传送门 de了两个小时的bug愣是没发现错在哪里……没办法只好重打了一遍竟然1A……我有点想从这里跳下去了…… 和方格取数问题差不多,把格子按行数和列数之和的奇偶性分为黑的和白的,可以发现某种颜色一定 ...

  5. iOS 开发之 GCD 不同场景使用

    header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...

  6. php常用的系统函数大全

    字符串函数 strlen:获取字符串长度,字节长度 substr_count 某字符串出现的次数 substr:字符串截取,获取字符串(按照字节进行截取) mb_strlenmb_substr str ...

  7. 一步步yum安装LNMP,脱坑笔记!!!

    更改国内yum源: 1.备份yum源文件,位置在/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/ ...

  8. 配置YOLO2(ubuntu16.04+cuda8.0+opencv3.1.0)

    要求已经安装好了CUDA 8.0 以及OpenCV3.1.0 YOLO官方网站 配置darknet git clone https://github.com/pjreddie/darknet cd d ...

  9. PyQt 5+qtDesigner

    https://blog.csdn.net/view994/article/details/84402069 https://blog.csdn.net/yizhou2010/article/deta ...

  10. springMVC下载功能

    前台页面 <a href="download">下载</a> 后台代码 /** * 文件下载 * @param request * @return * @t ...