立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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;
int main(){int M,N,K;scanf("%d%d%d",&M,&N,&K);while(K--){int A[N],t=0;//数组A存储出栈顺序,t存储下一次压栈时的首元素-1stack<int>s;bool f=true;//判断输出YES或者NOfor(int i=0;i<N;++i)scanf("%d",&A[i]);for(int i=0;i<N&&f;++i){//遍历数组Aif(s.empty()||s.top()<A[i]){//栈为空或者栈顶元素小于A[i]for(int k=t+1;k<=A[i]&&s.size()<M;++k)//将t+1~A[i]这些数按从小到大顺序压入栈中,并保证栈的当前容量不超过Ms.push(k);t=s.empty()?0:s.top();//更新tif(t==A[i])//栈顶元素与A[i]相等s.pop();//弹出else//栈顶元素与A[i]不等f=false;//置f为false}else if(s.top()==A[i])//栈不为空且栈顶元素等于A[i]s.pop();//弹出栈顶元素else//栈不为空且栈顶元素大于A[i]f=false;//置f为false}printf("%s\n",f?"YES":"NO");//输出}return 0;
}

耗时:

【详细注释】1051 Pop Sequence (25 分)相关推荐

  1. 【PAT (Advanced Level) Practice】1051 Pop Sequence (25 分)

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

  2. 【PAT甲】1051 Pop Sequence (25分)判断出栈顺序的合法性

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

  3. 【PAT甲级 stack queue的使用】1051 Pop Sequence (25 分) C++ 全部AC

    题目 思路 维护三个集合:核心栈.数字队列.目标队列 详细思路见注释 题解 C++ #include<iostream> #include<stack> #include< ...

  4. PAT (Advanced Level) 1051. Pop Sequence (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  5. PAT甲级1051 Pop Sequence:[C++题解]模拟栈、判断序列是否是合法的出栈序列

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 题意:将1~N压栈,判断给定序列是否是合法的出栈序列. 对于序列1~N中的每个值i,先将其压入栈.然后对于它就有两种处理方法:要么压 ...

  6. 【详细注释】1058 选择题 (20 分)

    立志用最少的代码做最高效的表达 PAT乙级最优题解-->传送门 批改多选题是比较麻烦的事情,本题就请你写个程序帮助老师批改多选题,并且指出哪道题错的人最多. 输入格式: 输入在第一行给出两个正整 ...

  7. 1051 Pop Sequence(两种双指针思路)

    目录 思路一:以入栈序列为总纲,2层循环,外for内while 思路二:一层while 思路一:以入栈序列为总纲,2层循环,外for内while 注意弹栈之前要判空,不然会出现段错误. AC代码 #i ...

  8. Pop Sequence

    02-线性结构4 Pop Sequence   (25分) Given a stack which can keep MMM numbers at most. Push NNN numbers in ...

  9. C++学习之路 | PTA(甲级)—— 1043 Is It a Binary Search Tree (25分)(带注释)(精简)

    1043 Is It a Binary Search Tree (25分) A Binary Search Tree (BST) is recursively defined as a binary ...

最新文章

  1. HTML5中aside标签的两种使用方法
  2. word count in latex, relatively accurate
  3. Discuz!NT控件剖析 之 Button [原创: 附源码]
  4. Codis集群的搭建与使用
  5. Qtum量子链研究院:Plasma扩容方案详解(上)
  6. 【矩阵乘法】递推(ssl 1532)
  7. VC中Error spawning cl.exe错误的解决方法.
  8. Android studio3.5 SDK29项目开发笔记
  9. 【LeetCode】【HOT】102. 二叉树的层序遍历(队列)
  10. python装饰器测试_python 装饰器
  11. linux下iconv命令转换字符编码
  12. 境内区块链信息服务备案清单(第三批)
  13. 序列标注--词性英文对照表
  14. java参数传递内存图解,探讨Java参数传递问题
  15. C++查詢wry.dat中的IP地址信息
  16. python对月饼数据进行可视化,看看哪家最划算
  17. 集体照的拍摄及后期合成
  18. seaborn绘图基础介绍
  19. SQL39 针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005,使用强制索引。
  20. MQTT - 消息队列遥测传输协议

热门文章

  1. C++ 面向对象(二)多态 : 虚函数、多态原理、抽象类、虚函数表、继承与虚函数表
  2. 使用 ebpf 深入分析容器网络 dup 包问题
  3. 2023年多播ABR市场将达8亿美元
  4. 多媒体技术创新与难点探索(内附讲师资料下载)
  5. 分布式之系统底层原理
  6. 腾讯计费:亿万级大促活动自动化保障体系
  7. TCP/IP详解--TIME_WAIT状态存在的原因
  8. CFA将人工智能列入考试内容折射啥
  9. android jni开发方式
  10. 更改idea控制台输出字体