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 ...
随机推荐
- c# 解析MP3文件
不说那么多,网上有很多关于MP3文件说明的. 该C#代码是将C\C++转化过来的,可能存在问题. 如有需要的朋友可以参考. 项目中的文件路径大家自己修改. 下载地址
- 强制json格式
- Codeforces Round #549 (Div. 2)A. The Doors
A. The Doors time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Kubernetes 集群部署(3) -- Flannel 集群
1. 下载包 wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64. ...
- 入参为json类型的接口测试示例
一.接口文档内容 二.使用postman(入参为json类型) 如下部分内容即是json串: { "name":"刘星", "grade": ...
- 洛谷P2766 最长不下降子序列问题(最大流)
传送门 第一问直接$dp$解决,求出$len$ 然后用$f[i]$表示以$i$为结尾的最长不下降子序列长度,把每一个点拆成$A_i,B_i$两个点,然后从$A_i$向$B_i$连容量为$1$的边 然后 ...
- js闭包引起的事件注册问题
背景:闲暇时间看了几篇关于js作用域链与闭包的文章,偶然又看到了之前遇到的一个问题,就是在for循环中为dom节点注册事件驱动,具体见下面代码: <!DOCTYPE html> <h ...
- loj #2538. 「PKUWC2018」Slay the Spire
$ \color{#0066ff}{ 题目描述 }$ 九条可怜在玩一个很好玩的策略游戏:Slay the Spire,一开始九条可怜的卡组里有 \(2n\) 张牌,每张牌上都写着一个数字\(w_i\) ...
- SDUT OJ 数据结构实验之链表一:顺序建立链表
数据结构实验之链表一:顺序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- TX2中设备树烧写
将要修改的设备树文件拷贝到下面的目录替换相应的文件 ../64_TX2/Linux_for_Tegra_tx2/kernel/dtb 用micro-USB线连接TX2上的USB OTG口和PC机的US ...