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 ...
随机推荐
- 创建Oracle synonym 详解
--创建使用同义词 --同义词就是给表.视图等对象取得别名,用于简化对其的访问 --分为2种: --私有同义词:用户自己创建自己使用的 --公共同义词:dba创建,给其它用户使用的 --为dept_s ...
- 谈谈easyui datagrid 的数据加载
文章目录 1url方式加载数据 1.1调用方式 1.2相关方法 1.3二次加载问题 2加载本地数据方式 2.1调用方式 2.2如何分页 2.3加载中效果 2.4如何不统计总数 这篇文章只谈jQuery ...
- 26.Generate Parentheses(生产有效括号的种类)
Level: Medium 题目描述: Given n pairs of parentheses, write a function to generate all combinations of ...
- 利用EFCore 封装Repository(可扩展不同数据的sql操作)
本篇是对EFCore 进行下封装并实现基本的增删改查的同步异步方法及针对不同数据库的批量插入.sql语句直接操作数据库: 一. 先定义基础仓储接口IRepository public interfac ...
- Android 给app加入百度地图
1.获取sha1值 (1)win+R进入cmd窗口 (2)输入以下代码 C:\SoftApplication\javajdk\jdk1.8.0_151\bin>keytool -list -v ...
- mysql数据库怎么使用,mysql的使用方法
https://jingyan.baidu.com/article/5d368d1ec069c13f61c05742.html 数据库的开启与关闭: https://blog.csdn.net/u01 ...
- C++_基础4-分支语句和逻辑运算符
这一部分截取自<C++ Primer Plus>,内容比较简单,很多只取了一些主题关键词,有空再补充: 设计智能程序的一个关键是使程序具有决策能力. 前面一种方式是循环——程序决定是否继续 ...
- bzoj4548: 小奇的糖果 题解
题目链接 题解 不包含所有颜色 就强制不选一个颜色 图中圆点颜色相同 矩形越大,包括的点一定不比其一小部分少 如图所示,最大矩形只有3种 离散化\(x\)坐标 然后按\(y\)排序 每次取出颜色的前驱 ...
- Oracle9i之xmltype应用(2)
Oracle 9i提供的XML内置特性: Oracle 9i支持XMLType类型,它是一种Oracle 9i系统定义的对象类型.XMLType有内置的函数,有力的提供了推XML的创建,索检,索引等功 ...
- 豆瓣模拟登录(双层html)
一.豆瓣模拟登录(双层html) #!/usr/bin/env python # -*- coding: utf-8 -*- #author tom import time from selenium ...