pat02-线性结构4. Pop Sequence (25)
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 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)的更多相关文章
- 线性结构4 Pop Sequence
02-线性结构4 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the or ...
- 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 ...
- 数据结构练习 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 ...
- 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 ...
- 02-线性结构4 Pop Sequence
02-线性结构4 Pop Sequence (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...
- 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 ...
- 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 ...
- PAT 1051 Pop Sequence (25 分)
返回 1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ...
- 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 ...
随机推荐
- jstack应用-查找CPU飚高的原因
场景 在系统上线后,经常会遇到运维的同学跑过来说:“这次发版后,cpu线程使用率到一场,到100%了”.这时候不要慌,可以使用堆转储来分析到底是哪个线程引起的. 查找元凶 发现pid=17850的进程 ...
- B - N皇后问题
原文链接 一天课下,张老板研究起了国际象棋,渴望完美的他更改了棋盘的大小,在N*N的方格棋盘放置了N个皇后,希望它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的 ...
- javascript立即执行函数与模块化
概念:立即执行函数顾名思义就是函数定义好之后立即执行.函数表达式方式:函数表达式后面加括号()即可立即执行函数. var xmlhttpUtil = function () { function ge ...
- DHCP与PPPOE 区别
1.静态IP的方式,如果是占用一个INTERNET的IP的话,上网都是很贵的,当然这个也是最方便的,开机就能上网,不用做任何拨号或者认证的过程.2.PPPOE,只是多了一个获得IP的过程,一旦获得了I ...
- TX2 默认root用户启动
Jetpack3.1 修改方式 修改1 gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 修改后: 修改2 gedit /root/.pro ...
- Qt 学习之路 2(45):模型
Home / Qt 学习之路 2 / Qt 学习之路 2(45):模型 Qt 学习之路 2(45):模型 豆子 2013年2月26日 Qt 学习之路 2 23条评论 在前面两章的基础之上,我们 ...
- Jprofiler的安装部署及使用
本地与远程安装同版本的jprofiler.以本地Windows操作系统,远程AIX操作系统为例,详细介绍安装配置步骤.本次测试使用的均是jp6版本. 一.安装Jprofiler服务端 一 般情况下,J ...
- php 替换 oracle 数据字段中“看不见”换行符号
工作需要,把oracle中的数据导出csv,导出代码如下:<?php$file_name = "申請書承認(予定休出).csv";header("Content-D ...
- 2016 ccpc 杭州 D.Difference hdu5936(折半枚举)
有坑!!!当x==0时,因为y>0,a,b不能同时为0,所以答案要-1 #include<iostream> #include<cstdlib> #include< ...
- 二叉堆(小到大)-数据结构-JavaScript版
/** * Created by caoke on 2015/11/21. */ //二叉树 特点父节点比子节点小 var Tree2=function(){ //初始化 二叉树的子元素 this.c ...