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 分)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

    题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...

  4. 1051 Pop Sequence (25分)栈

    刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...

  5. 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 ...

  6. 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 ...

  7. PAT 1051 Pop Sequence[栈][难]

    1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order ...

  8. 【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 ...

  9. 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. CentOS7 防火墙Firewall常用命令

    1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld  停止: systemctl disab ...

  2. "如何用70行Java代码实现深度神经网络算法" 的delphi版本

     http://blog.csdn.net/hustjoyboy/article/details/50721535 "如何用70行Java代码实现深度神经网络算法" 的delphi ...

  3. 基于windows下,node.js之npm

    1.下载node.js一路安装下去 在开始 node文件夹下,打开cmd 2.创建一个开发目录 mkdir reactQa && cd reactQa 3.初始化一个nmp的开发环境 ...

  4. 转 : jconsole 和jvisualVM 监控远程 spring boot程序

    监控java 程序 增加启动参数 java  \ -Djava.rmi.server.hostname=192.168.2.39 \ -Dcom.sun.management.jmxremote \- ...

  5. web 项目引入 maven jar 工具类异常

    普通的web 项目引入 maven   子项目后,,启动web不会出现异常,登录web 页面异常提示: HTTP Status 500 - java.lang.NoSuchMethodError: o ...

  6. 各种Web服务器与Nginx的对比

    Tomcat和Jetty面向Java语言,先天就是重量级的Web服务器,它们的性能与Nginx没有可比性. IIS只能在windows操作系统上运行,Windows作为服务器在稳定性与其他一些性能上都 ...

  7. k8s 组件介绍-API Server

    API Server简介 k8s API Server提供了k8s各类资源对象(pod,RC,Service等)的增删改查及watch等HTTP Rest接口,是整个系统的数据总线和数据中心. kub ...

  8. Java-技术专区-如何监控Java线程池的状态

    线程池介绍 什么是线程池.线程池核心类.线程池工作流程.线程池分类.拒绝策略.及如何提交与关闭线程池等. 但在实际开发过程中,在线程池使用过程中可能会遇到各方面的故障,如线程池阻塞,无法提交新任务等. ...

  9. 50.Maximal Square(最大正方形)

    Level   Medium 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square conta ...

  10. 一些常ArcGIS常用简单算法 C#

    最近开始重构不顺眼的辣鸡代码,顺带将某个模块的一个算法辅助类贴到这里. /// <summary> /// 算法逻辑辅助类 /// </summary> internal st ...