PAT 甲级 1051 Pop Sequence
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的更多相关文章
- 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 ...
- 【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甲级——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 ...
- 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 ...
- 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 ...
- 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 分)
返回 1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ...
- 1051. Pop Sequence
原题连接:https://www.patest.cn/contests/pat-a-practise/1051 题目: Given a stack which can keep M numbers a ...
- 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 ...
随机推荐
- 基于TP5使用Websocket框架之GatewayWorker开发电商平台买家与卖家实时通讯
https://www.cnblogs.com/wt645631686/p/7366924.html 前段时间公司提了一个新的需求,在商品的详情页要实现站内买家和商品卖家实时通讯的功能以方便沟通促成交 ...
- 3.3 线程---Handler消息传递机制浅析
Handler的执行流程图: 当我们的子线程想修改Activity中的UI组件时,我们可以新建一个Handler对象,通过这个对象向主线程发送信息;而我们发送的信息会先到主线程的MessageQueu ...
- Python os.walk() 方法
#coding=utf-8 import os #(dirpath, dirnames, filenames)[文件夹路径, 文件夹名字, 文件名] def file_name(file_dir): ...
- Java 将任意数组的任意两个位置的数据进行交换
package yw.fanxing; /** * 自定义泛型测试 * * 写一个方法,将任意数组的任意两个位置的数据进行交换 * * @author yw-tony * */ public clas ...
- kubernetes 限制网络带宽 annotation -- 注解 -- 类似 label
1.可以通过给Pod增加 kubernetes.io/ingressbandwidth和 kubernetes.io/egress-bandwidth 这两个annotation来限制Pod的网络带宽 ...
- 20155308『网络对抗技术』Exp7:网络欺诈防范
20155308『网络对抗技术』Exp7:网络欺诈防范 原理与实践说明 1.实践目标 本实践的目标是:理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. 2.实践内容概述 简单应用SET ...
- WPF编程,通过DoubleAnimation控制图片的透明度,将重叠的图片依次显示。
原文:WPF编程,通过DoubleAnimation控制图片的透明度,将重叠的图片依次显示. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307 ...
- Luogu P1993 小 K 的农场
其实很早以前就打好了,但一直忘记写了. 也就是差分约束的模板题. 关于差分约束,也就是用来求关于一些不等式互相约束算出最优解. 推荐一个讲的很好的博客:http://www.cppblog.com/m ...
- [SPOJ2939]Qtree5
[SPOJ2939]Qtree5 Tags:题解 题意 链接 给你\(n\)个节点的黑白树,初始全黑.每次可以翻转某点颜色,或查询距离某点最近的白点的距离.\(n\le 10^5\).强制LCT,不准 ...
- [APIO2013]机器人[搜索、斯坦纳树]
题意 题目链接 分析 记 g(d,x,y) 表示从 (x,y) 出发,方向为 d 到达的点,这个可以通过记忆化搜索求出,注意如果转移成环(此时向这个方向走没有意义)要特判. 记 f(l,r,x,y) ...