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 ...
随机推荐
- (转)Docker 网络
转:https://www.cnblogs.com/allcloud/p/7150564.html 本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker ...
- Gradle Wrapper 介绍
回顾 上一篇介绍了如何使用 Gradle 快速创建 Java 项目,开始讲 Gradle Wrapper 之前先来重温一下通过 gradle init 命令创建的 Java 项目目录结构 ├── bu ...
- Hibernate 异常org.hibernate.LazyInitializationException: could not ini...
错误页面提示 could not initialize proxy - no Session 控制台 org.hibernate.LazyInitializationException: could ...
- 算法中时间复杂度概括——o(1)、o(n)、o(logn)、o(nlogn)
在描述算法复杂度时,经常用到 o(1), o(n), o(logn), o(nlogn) 来表示对应算法的时间复杂度, 这里进行归纳一下它们代表的含义:这是算法的时空复杂度的表示.不仅仅用于表示时间复 ...
- selenium:Xpath定位详解
xpath定位在业界被戏称为元素定位的"屠龙宝刀",宝刀在手,武林我有.现在我们就来详解xpath定位方法. 一.xpath通过元素属性定位 xpath可以通过元素的属性来定位,如 ...
- mysql FROM_UNIXTIME 时间不准确
mysql 使用 FROM_UNIXTIME 函数计算出来的时间少了6个小时或者8个小时 解决办法: 添加 default-time_zone = '+8:00' 这个再配置文件中 vi /etc/m ...
- javascript中数组的应用总结
最近在总结javascript的相关应用,今天对js中的数组部分进行归纳总结,以便在以后的工作中有所参考. 1.在js中数组的定义方式有两种: var a = [1,2,3,4]; var b = n ...
- Python基础篇(初始函数)
Python初始函数: 一.什么是函数 1.我们到目前为止, 已经可以完成一些软件的基础功能了. 那么我们来完成这样一个功 能: 约x: print("拿出手机") print(& ...
- webpack插件之html-webpack-plugin
官方文档:https://www.npmjs.com/package/html-webpack-plugin html-webpack-plugin 插件专门为由webpack打包后的js提供一个载体 ...
- linux文件管理--压缩打包
目录 linux文件管理--压缩打包 1.压缩打包介绍 2.gzip压缩工具 3.zip压缩工具 注意: 4.tar压缩工具 5.tar生产案例实践 linux文件管理--压缩打包 1.压缩打包介绍 ...