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 ...
随机推荐
- Python 简单说明与数据结构
Python 简单说明与数据结构 Python 作为 "国内" 较流行的高级语言,具有代码容易理解.专注解决问题.混合编译其他语言的优点. 变量 变量是一个最基本的储存单位,它暂时 ...
- PC站与H5移动站最佳适配方案
HTML5是目前HTML的最屌版本,同时也是建设移动站的最佳技术.百度适时推出PC站与H5移动站的最佳适配方案,对站长而言实在是久旱逢甘霖.详情如下: PC站与H5移动站最佳适配方案 pc端: 在pc ...
- 一个数字从后向前输入每一位数字,Camel和Pascal命名规范,IsValid()
int num = int.Parse(Console.ReadLine()); ; ) { n = num % ; num /= ; Console.WriteLine(n); } Camel和Pa ...
- python学习之路---day19--面向对象--约束
约束和异常一:约束: 约束方法:(解释:就相当于在想项目中,先提前规划好框架和约定基本代码,像定义好调用的名字后,后面的名字只能用这个,用其他的 名字就会抛出异常.错误) 1:提取父类,然后再父类中定 ...
- Python web前端 06 运算符 循环
Python web前端 06 运算符 循环 一.运算符 #JS六大数据类型 #number 数字 #string 字符串 #boolean 布尔型 #function 函数 #undefined 未 ...
- C++_异常5-异常规范和栈解退
异常规范 异常规范的理念看似有前途,但实际的使用效果并不好. 忽视异常规范之前,您至少应该知道它是什么样的,如下所示: double harm(double a) throw(bad_thing); ...
- Codeforces - 600E 树上启发式合并
题意:求每一个子树存在最多颜色的颜色代号和(可重复) 本题是离线统计操作,因此可以直接合并重儿子已达到\(O(nlogn)\)的复杂度 PS.不知道什么是启发式合并的可以这样感受一下:进行树链剖分,分 ...
- 关于XML的验证(DTD与XSD)一点实践
[转自] http://blog.chinaunix.net/uid-276853-id-366491.html 关于XML的验证一点实践 1)此方法是在XML文档中绑定对应的DTD文件来进行的 // ...
- Robot Framework变量的使用技巧
1.变量的使用 变量可以在命令行中设置,个别变量设置使用--variable (-v)选项,变量文件的选择使用--variablefile (-V)选项.通过命令行设置的变量是全局变量,对其所有执行的 ...
- C/S架构与B/S架构的区别
什么是C/S结构和B/S结构? C/S结构 C/S结构是指Client/Server (客户机/服务器) 结构,是大家熟知的软件系统体系结构,通过将任务合理分配到Client端和Server端,降低了 ...