https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944

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 N, M, K;
vector<int> v; int main() {
scanf("%d%d%d", &M, &N, &K);
while(K --) {
bool flag = false;
v.resize(N + 1);
int cnt = 1;
for(int i = 1; i <= N; i ++)
scanf("%d", &v[i]); stack<int> s;
for(int i = 1; i <= N; i ++) {
s.push(i);
if(s.size() > M) break;
while(!s.empty() && s.top() == v[cnt]) {
s.pop();
cnt ++;
}
} if(cnt == N + 1) flag = true;
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

  感觉年就这么结束了诶

 

PAT 甲级 1051 Pop Sequence的更多相关文章

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

  3. PAT甲级——A1051 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 ...

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

  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. PAT 1051 Pop Sequence[栈][难]

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

  7. PAT 1051 Pop Sequence (25 分)

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

  8. 1051. Pop Sequence

    原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...

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

随机推荐

  1. php 微信公众号接入支付宝支付

    真是无力吐槽这个需求了,好端端的非要在微信公众号接入支付宝,都知道微信公众号是拒绝支付宝的,屏蔽了支付宝,所以在微信公众号接入支付宝的话就必须手动复制链接跳出微信内置浏览器,强制性打开web浏览器完成 ...

  2. Kafka学习之路 (五)Kafka在zookeeper中的存储

    一.Kafka在zookeeper中存储结构图 二.分析 2.1 topic注册信息 /brokers/topics/[topic] : 存储某个topic的partitions所有分配信息 [zk: ...

  3. WorldWind源码剖析系列:星球类World

    星球类World代表通用的星球类,因为可能需要绘制除地球之外的其它星球,如月球.火星等.该类的类图如下. 需要说明的是,在WorldWind中星球球体的渲染和经纬网格的渲染时分别绘制的.经纬网格的渲染 ...

  4. 回调函数ros::spin()与ros::spinOnce()

    ros::spin() 这句话的意思是循环且监听反馈函数(callback).循环就是指程序运行到这里,就会一直在这里循环了.监听反馈函数的意思是,如果这个节点有callback函数,那写一句ros: ...

  5. 织梦提交表单(提交留言)前的js校验

    第一种情况:页面有引入jq的 在form标签上加上id <form action="/plus/diy.php" enctype="multipart/form-d ...

  6. Kafka设计解析(二十三)关于Kafka监控方案的讨论

    转载自 huxihx,原文链接 关于Kafka监控方案的讨论 目前Kafka监控方案看似很多,然而并没有一个“大而全”的通用解决方案.各家框架也是各有千秋,以下是我了解到的一些内容: 一.Kafka ...

  7. haproxy [WARNING] 312/111530 (17395) : config : 'option forwardfor' ignored for frontend 'harbor_login' as it requires HTTP mode.

    1.经过调查, 2down voteaccepted x-forwarded-for is an HTTP header field, so has nothing to do with the tr ...

  8. H.264从SPS中提取视频宽高

    H.264有两种封装模式: (1)annexb模式:传统模式,使用start code来分隔NAL, SPS和PPS是在ES流的头部: (2)mp4模式:没有start code,使用NALU长度(固 ...

  9. C# 分部方法partial

    定义:使用partial关键字构建分部类定义.允许在一个文件中构建方法原型,而在另一个文件中实现. 1)分部方法只可以定义在分部类中 2)分部方法必须返回void 3)分部方法可以是静态的或实例级别的 ...

  10. Scala--集合

    一.主要的集合特质 Seq有先后顺序的序列,如数组列表.IndexedSeq通过下标快速的访问元素.不可变:Vector, Range, List 可变:ArrayBuffer, LinkedList ...