#include <cstdio>
#include <cstdlib>
#include <vector> using namespace std; bool push_validate(int &pre_push, int max_push, int cur, vector<int>& stack, int mcap) {
if (pre_push >= max_push || pre_push >= cur) {
// there not exist valid push for this pop
return false;
}
if (cur > max_push) {
// this pop value is out of range
return false;
}
if (stack.size() + cur - pre_push > mcap) {
// stack capacity not enough
return false;
}
for (int j = pre_push+; j<=cur; j++) {
// push the value (if less value not pushed also push them in)
stack.push_back(j);
}
pre_push = cur;
return true;
} bool validate(vector<int> &seq, int capacity) {
int len = seq.size();
int pre_push = ;
int max_push = len; vector<int> stack;
int i = ;
while (i<len) {
int cur = seq[i];
//printf("cur seq: %d\n", cur);
if (stack.empty() || cur != stack.back()) { // there must be a push before this pop or it's invalid
if (push_validate(pre_push, max_push, cur, stack, capacity)) {
continue;
} else {
return false;
}
}
// easy case, just pop element from stack & check next in the pop seq
i++;
//printf("pop %d\n", stack.back());
stack.pop_back();
}
return true;
} int main() {
int M, N, K;
scanf("%d%d%d", &M, &N, &K);
vector<int> seq(N);
for (int i=; i<K; i++) {
for (int j=; j<N; j++) {
scanf("%d", &seq[j]);
} if (validate(seq, M)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return ;
}

以前考PAT时做过当时不知什么原因好像没全对,这回一次过

PAT 1051 Pop Sequence的更多相关文章

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

    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 分)

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

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

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

  5. PAT 甲级 1051 Pop Sequence

    https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944 Given a stack which ca ...

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

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

  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. Scala详细环境安装与配置

    https://blog.csdn.net/free356/article/details/72911898 系统为windows.安装配置Scala如下: 一,安装Scala 1,java6以上(建 ...

  2. how to use windows azure market

    here is the sample. namespace USCrime2006and2007 { class Program { static void Main(string[] args) { ...

  3. SDUT OJ 数据结构实验之二叉树二:遍历二叉树

    数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  4. K3 Wise 常用表【转载】

    在后台数据库ICClassType表中,字段FID<0的是老单,FID>0的是新单.--事务类型select * from ICClassType            where  FT ...

  5. javascript的代码块

    a block of code 注意到这个问题是在看书的时候,中文版中出现“代码片段”这样的词语,于是就去翻看了英文版的原书.书中的用了a block of code,难道不应该翻译成代码块吗?(作为 ...

  6. 把磁力下载站改为python系统

    已经一年半载没有写博客了,搞得上来不知道写些什么. 索马里影视下载  WWW.IBMID.COM  现在用的是CENTOS 7 系统, 经历了多次点技术变更.开源版本使用了django网站框架重写,之 ...

  7. Luogu P1282 多米诺骨牌 DP。。背包?

    背包...差不多..QWQ 设f[i]为达到差值为i的状态需要多少次,那就很显然了: 注意区分正负不同的代价的循环方向 技巧:如果不想改负数的话,那可以移动一下数组下标,用一个新的指针指向原来的数组 ...

  8. Qt随笔 - QSettings

    QSettings类提供了持久的跨平台应用程序设置. 嗯,一句话概括QSettings-- 创建 来看一下原型: QSettings::QSettings(const QString &org ...

  9. Django settings配置文件

    由来:为什么我在用django配置的时候导入的不是我项目名下的那个settings 但是我配置了之后依然能够起作用,这是为什么? from django.conf import settings # ...

  10. 2019.3.20 I/O相关

    I/O 相关简介 什么是I/O? IO,即Input (输入)和Output (输出)的首字母缩写. 什么是流? 流(Stream)是抽象概念,它代表任何有能力产出数据的数据源对象或者是与能力接收数据 ...