PAT 1051 Pop Sequence
#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的更多相关文章
- 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 ...
- 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 (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
https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944 Given a stack which ca ...
- 【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 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 ...
- 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 ...
随机推荐
- Java设计模式之单例设计模式 入门实例
一.基础概念 (1).单例设计模式:保证一个类在内存中的对象唯一性. (2).应用场景:数据都存储在配置文件的对象中,多个程序对同一个配置文件的对象进行操作.一个程序要基于另一个程序操作后的结果进行操 ...
- 命令行里打 cd 简直是浪费生命
简评:作为工程师,你在命令行下最常打的命令无非就是 cd 与 ls.这些年你浪费了多少时间? 作为一个程序员或者在 shell 中花费大量时间的人,你可能会经常以一种低效率的方式在目录中来回移动,特别 ...
- zabbix 3.0 快速安装文档
下载地址:http://www.zabbix.com/download.php 官方文档:https://www.zabbix.com/documentation/3.0/manual/install ...
- Reviewing notes 1.1 of Advanced algebra
♦Linear map Definition Linear map A linear map from vector space V to W over a field F is a function ...
- HTTP上下文表单内容转为实体对象
using ServiceStack.Web; using System; using System.Collections.Generic; using System.Linq; using Sys ...
- LOJ #6432. 「PKUSC2018」真实排名
题目在这里...... 对于这道题,现场我写炸了......谁跟我说组合数O(n)的求是最快的?(~!@#¥¥%……& #include <cstdio> #include < ...
- JAVA数据结构--LinkedList双向链表
链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括两个部分: ...
- kerl build error
删除 archives文件夹就行了
- Sasha and a Bit of Relax(前缀异或和+二维数组+思维)
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and ...
- HDU-6341 Problem J. Let Sudoku Rotate(dfs 剪枝)
题目:有一个4*4*4*4的数独,每一横每一竖每一个小方块中都无重复的字母,即都为0-9,A-F..有一个已经填好的数独,若干个4*4的方块被逆时针拧转了若干次,问拧转回来至少需要多少次. 分析:很明 ...