https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.

Output Specification:

For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.

Sample Input:

  1. 5 7 5
  2. 1 2 3 4 5 6 7
  3. 3 2 1 7 5 6 4
  4. 7 6 5 4 3 2 1
  5. 5 6 4 3 7 2 1
  6. 1 7 6 5 4 3 2

Sample Output:

  1. YES
  2. NO
  3. NO
  4. YES
  5. NO

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int N, M, K;
  5. vector<int> v;
  6.  
  7. int main() {
  8. scanf("%d%d%d", &M, &N, &K);
  9. while(K --) {
  10. bool flag = false;
  11. v.resize(N + 1);
  12. int cnt = 1;
  13. for(int i = 1; i <= N; i ++)
  14. scanf("%d", &v[i]);
  15.  
  16. stack<int> s;
  17. for(int i = 1; i <= N; i ++) {
  18. s.push(i);
  19. if(s.size() > M) break;
  20. while(!s.empty() && s.top() == v[cnt]) {
  21. s.pop();
  22. cnt ++;
  23. }
  24. }
  25.  
  26. if(cnt == N + 1) flag = true;
  27. if(flag) printf("YES\n");
  28. else printf("NO\n");
  29. }
  30. return 0;
  31. }

  感觉年就这么结束了诶

 

PAT 甲级 1051 Pop Sequence的更多相关文章

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

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

  3. PAT甲级——A1051 Pop Sequence

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

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

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

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

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

  7. PAT 1051 Pop Sequence (25 分)

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

  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. zabbix(2-server-agent)

    注意:以下步骤都是在LAMP配置之后进行的. 关于LAMP环境的简单快速搭建,见博客:http://afterdawn.blog.51cto.com/7503144/1923139 下面开始介绍在Ce ...

  2. DQN(Deep Reiforcement Learning) 发展历程(二)

    目录 动态规划 使用条件 分类 求解方法 参考 DQN发展历程(一) DQN发展历程(二) DQN发展历程(三) DQN发展历程(四) DQN发展历程(五) 动态规划 动态规划给出了求解强化学习的一种 ...

  3. 虚函数指针sizeof不为sizeof(void*)

    ref:http://bbs.csdn.net/topics/360249561 一个继承了两个虚基类又增加了自己的一个虚函数pif的类,sizeof(指向pif的指针)竟然是8(X86).我是从这里 ...

  4. ASP.NET Core的Kestrel服务器(转载)

    Kestrel是一个基于libuv的跨平台ASP.NET Core web服务器,libuv是一个跨平台的异步I/O库.ASP.NET Core模板项目使用Kestrel作为默认的web服务器.Kes ...

  5. 20155330 《网络对抗》 Exp6 信息搜集与漏洞扫描

    20155330 <网络对抗> Exp6 信息搜集与漏洞扫描 基础问题回答 哪些组织负责DNS,IP的管理? 互联网名称与数字地址分配机构(The Internet Corporation ...

  6. 20155330 《网络对抗》 Exp5 MSF基础应用

    20155330 <网络对抗> Exp5 MSF基础应用 实践过程记录 主动攻击实践:MS08_067漏洞攻击 攻击机:kali IP地址:192.168.124.132 靶机:windo ...

  7. POJ 3278&&2049&&3083

    这次的题目叫图的深度&&广度优先遍历. 然后等我做完了题发现这是DFS&&BFS爆搜专题. 3278:题目是经典的FJ,他要抓奶牛.他和牛(只有一头)在一条数轴上,他们 ...

  8. leetcode刷题笔记258 各位相加

    题目描述: 给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字. 例如: 设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返 ...

  9. 使用devstack/pike部署多节点实验

    目录 第一步:安装Ubuntu16.04 server并以stack为用户名创建用户 第二步:安装git及相关配置 第三步:安装Open vSwitch 2.5.X 第四步:获取devstack脚本 ...

  10. DRF03

    为了方便接下来的操作,需要在admin站点创建一个管理员. python manage.py createsuperuser 可在setting.py中修改admin站点语言, LANGUAGE_CO ...