#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. [Swift实际操作]九、完整实例-(5)创建BaseViewController作为控制器的基类

    本文将给项目中的所有视图控制器,创建一份基类.该基类用来定义一些共用的属性和方法. 首先在用来放置视图控制器类的文件夹上点击鼠标右键,打开右键 菜单. 选择[New File]创建文件选项. 在弹出的 ...

  2. time、random以及序列化模块

    一. time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type( ...

  3. Anya and Cubes 搜索+map映射

    Anya loves to fold and stick. Today she decided to do just that. Anya has n cubes lying in a line an ...

  4. angular-ui-select 下拉框支持过滤单选多选解决方案(系列一)

    angular-ui-select  官方文档:github地址:https://github.com/angular-ui/ui-select 请大家多看文档     首先注意版本的问题,如果版本不 ...

  5. uiautomator中文输入

    步骤一: 下载UTF7 IME的整个项目 https://github.com/sumio/uiautomator-unicode-input-helper 选择“Download ZIP”打包下载 ...

  6. C++_标准模板库STL概念介绍4-算法

    STL包含很多处理容器的非成员函数: sort() copy() find() random_shuffle() set_union() set_intersection() set_differen ...

  7. java代码将excel文件中的内容列表转换成JS文件输出

    思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...

  8. 最小生成树的kruskal、prim算法

    kruskal算法和prim算法 都说 kruskal是加边法,prim是加点法 这篇解释也不错:这篇 1.kruskal算法 因为是加边法,所以这个方法比较合适稀疏图.要码这个需要先懂并查集.因为我 ...

  9. maven-javadoc-plugin

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javad ...

  10. html中的flv视频播放器

    项目中要播放flv视屏,第一时间想到html5的<video>标签,只是很可惜<video>兼容性差也就算了,居然还对格式有明确限制,也就是说只支持Ogg.MPEG4.WebM ...