PAT 1051 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<bits/stdc++.h>
using namespace std;
typedef long long ll;
int m,n,k;
int a[] = {}; int main(){
cin >> m >> n >> k;
while(k--){
int flag = ;
memset(a,,sizeof(a));
for(int i=;i < n;i++){
cin >> a[i];
}
for(int i=;i < n;i++){
int cnt = ;
int fflag = ;
int temp = ;
for(int j=i+;j < n;j++){
if(a[i] > a[j]){
cnt++;
if(a[j] > temp) fflag = ;
temp = a[j];
}
}
if(cnt > (m-) || fflag == ){
cout << "NO" << endl;
flag = ;
break;
}
}
if(flag) cout << "YES" << endl;
} return ;
}
合法出栈顺序首先得每个数后面不能有多于m-1的数比他小,不然装不下。
其次就是后面比他小的数一定要按递减排列
PAT 1051 Pop Sequence (25 分)的更多相关文章
- 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 ...
- 1051 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 ...
- 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)
题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...
- 1051 Pop Sequence (25分)栈
刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...
- 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 ...
- 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 ...
- PAT 1051 Pop Sequence[栈][难]
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)(25 分)
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- PAT Advanced 1051 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 ...
随机推荐
- 第六周学习总结&实验报告四
一.实验目的 (1)掌握类的继承 (2)变量的继承和覆盖,方法的继承,重载和覆盖的实现: 二.实验的内容 (1)根据下面的要求实现圆类Circle. 1.圆类Circle的成员变量:radius表示圆 ...
- sdb报告-10 错误问题定位
# sdb报告-10 错误问题定位在sdb 的集群环境中,如果面对的是一个高并发的操作场景,有时候会莫名其妙地报告 -10 错误. 在 sdb 的错误列表中,-10 错误代表:系统错误. 这是一个笼统 ...
- python接口自动化测试三十四:github上某接口测试平台及配置
TeserHome地址:https://testerhome.com/opensource_projects/60前端:https://github.com/pencil1/ApiTestWeb 实现 ...
- 基于windows下,node.js之npm
1.下载node.js一路安装下去 在开始 node文件夹下,打开cmd 2.创建一个开发目录 mkdir reactQa && cd reactQa 3.初始化一个nmp的开发环境 ...
- scrapy爬虫值Items
Items有哪些知识? 1.声明 import scrapy class Product(scrapy.Item): name = scrapy.Field() price = scrapy.Fiel ...
- vue-router 传递参数的几种方式
本文转载自:https://blog.csdn.net/crazywoniu/article/details/80942642 vue-router传递参数分为两大类 编程式的导航 router.pu ...
- Get column value of Flex Datagrid by QTP
' get the number of rows in the tablerowCount=Browser("Browser").FlexApplication("App ...
- 安装Consul服务中心
安装Consul服务中心 首先下载对应版本的安装程序.点击下载 我下载的是macOS64位版本,下载文件是一个ZIP文件,下载后解压缩到一个你喜欢的位置,以开发模式启动consul服务: #进入con ...
- 关于<a></a>标签里嵌套<a></a>标签的bug
当你用一个<a></a>标签时,在<a>标签中再插入一个<a></a>,此时,你会发现外面的<a>标签会重复出现,且有点混乱.找 ...
- Php 十六进制短浮点数转十进制,带符号位
/** * 十六进制浮点型转为十进制 * @param String $strHex 十六进制浮点数 * @return 十进制 */ public static function hexToDecF ...