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. FileTracker:error FTK1011编译错误的原因和解决办法

    原因: 今天创建好项目名字完成关了VS,后感觉文件夹名字不太对改了一下,后来程序就调试不了出现FileTracker:error FTK1011编译错误0.0,经过网络查询应该是路径问题 解决方法: ...

  2. Vue axios发送Http请求

    axios 1.cnpm install axios --save 2.在vue文件中引入,import Axios from 'axios' 3.使用,Axios.get(url).then((re ...

  3. 离线安装 python 第三方库

     离线安装 python 第三方库 首先你需要在联网的服务器上已经安装了一个第三方库,比如是paramiko,也就是说你已经执行了 pip install paramiko    ,小提示: 如果在安 ...

  4. 「BZOJ 3123」「SDOI 2013」森林「启发式合并」

    题意 你有一个森林,你需要支持两个操作 查询两个结点路径上权值第\(k\)小 两个点之间连一条边 强制在线,结点数\(\leq 8\times 10^4\) 题解 如果可以离线,这就是一个主席树板子题 ...

  5. CSS探案之 background背景属性剖析

    首先,我们先来看看两个css属性:background和background-color,对!就是这两位,相信大家在平时应该没少 麻烦人家把,反正我是这样,几乎也少会用到背景图,原因很简单:就是有点害 ...

  6. window.open()弹出窗口被拦截

    之前有个需求是输入一些配置,然后点击预览,通过接口保存配置并返回预览页面链接,在新页面中打开链接.后来测试一直说没有新页面打开,我一看,原来是被浏览器拦截了. 原因如下: 浏览器只有在认为click和 ...

  7. 12、OpenCV Python 图像梯度

    __author__ = "WSX" import cv2 as cv import numpy as np def lapalian_demo(image): #拉普拉斯算子 # ...

  8. 【离散数学】 SDUT OJ 集合的包含

    集合的包含 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 已知含n个元素的集合的子集 ...

  9. jquery.from帮助类

    /** * 将form里面的内容序列化成json * 相同的checkbox用分号拼接起来 * @param {obj} 需要拼接在后面的json对象 * @method serializeJson ...

  10. SJTU 机试 数学

    题目描述 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入描述: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出描述: 一个 ...