线性结构4 Pop Sequence
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, ..., Nand 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<iostream>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
int M,N,K;
int check(vector<int> &vi){
int m=,n=,cap=M+;
stack<int> sta;
sta.push();
while(n<N){
while(sta.size()<cap&&vi[n]>sta.top())
sta.push(++m);
if(sta.top()==vi[n++]) sta.pop();
else return ;
}
return ;
}
int main()
{
cin>>M>>N>>K;
vector<int> vi(N,);
for(int j=;j<K;j++){
for(int i=;i<N;i++){
int n;
cin>>n;
vi[i]=n;}
if(check(vi))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
vi.clear();
}
return ;
}
线性结构4 Pop Sequence的更多相关文章
- pat02-线性结构4. Pop Sequence (25)
02-线性结构4. Pop Sequence (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 02-线性结构4 Pop Sequence
02-线性结构4 Pop Sequence (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 https://pta.p ...
- 02-线性结构3 Pop Sequence
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...
- 数据结构练习 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 ...
- 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-线性结构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
题目 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 ...
- [刷题] PTA 02-线性结构4 Pop Sequence
模拟栈进出 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; ...
随机推荐
- 最短路之SPFA
解决存在<<<负环>>>的图的单源最短路径: 判断有无负环: 如果某个点进入队列的次数超过N次则存在负环(SPFA无法处理带负环的图) 这里,只介绍用bfs(深搜) ...
- bzoj 4909 [Sdoi2017]龙与地下城
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4909 题解 目前为止仅仅在LOJ上A掉这道题(Loj真快!) 当然不是标准做法 显然我们只要 ...
- Helga Hufflepuff's Cup CodeForces - 855C
Helga Hufflepuff's Cup CodeForces - 855C 题意:给一棵n个节点的树,要给每一个节点一个附加值,附加值可以为1-m中的一个整数.要求只能有最多x个节点有附加值k. ...
- Folding UVA - 1630
题目 ans[i][j]表示由原串第i个字符到第j个字符组成的子串的最短折叠长度如果从i到j本身可以折叠,长度就是本身长度或折叠后的长度的最小值***此处参考:http://blog.csdn.net ...
- 对protected修饰符的范围用代码说明(同时说明用protected修饰的属性,在继承时,一定程度上破坏了封装)
目录结构: 本类: 本包: 子孙类: 其他包:
- josephus 问题的算法(转载)
Josephus 问题: 一群小孩围成一个圈,任意假定一个数 m,从第一个小孩起,顺时针方向数,每数到第 m 个小孩时,该小孩便离开.小孩不断离开,圈子不断缩小,最后剩下的一个小孩便是胜利者.究竟胜利 ...
- 521 Longest Uncommon Subsequence I 最长特殊序列 Ⅰ
给定两个字符串,你需要从这两个字符串中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列).子序列可以通过删去字符串中的某些字符实现,但不能改变剩余 ...
- Spring的校验(Validator)
使用Spring校验的大体流程: 最首先要有配置文件xml的支持(spring_validate.xml).(当然在web.xml中要有对该xml的体现) <beans xmlns=" ...
- 洛谷P4013 数字梯形问题(费用流)
题意 $N$行的矩阵,第一行有$M$个元素,第$i$行有$M + i - 1$个元素 问在三个规则下怎么取使得权值最大 Sol 我只会第一问qwq.. 因为有数量的限制,考虑拆点建图,把每个点拆为$a ...
- js深拷贝与浅拷贝的区别及实现
1. 对于基本数据类型 其值在内存中占据着固定大小的空间,并被保存在栈内存中.当一个变量向另一个变量复制基本类型的值,会创建这个值的副本,并且我们不能给基本数据类型的值添加属性.其为深拷贝. 2. 对 ...