PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)

简介: PAT (Advanced Level) Practice - 1051 Pop Sequence(25 分)

题目链接:点击打开链接


题目大意:有个容量限制为 m 的栈,分别把1,2,3,...,n入栈,给出一个系列出栈顺序,问这些出栈顺序是否可能。


解题思路:用 vector 存储每次待检测的序列,用 stack 从1-n依次存储,每次存储时,先判断是否超过容量,以及模拟出栈顺序匹配比较。



AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
int main()
{
    int cap,n,k;
    while(~scanf("%d%d%d",&cap,&n,&k))
    {
        while(k--)
        {
            vector<int> v(n+1);
            for(int i=1;i<=n;i++) scanf("%d",&v[i]);
            stack<int> sk;
            int f=1,j=1;
            for(int i=1;i<=n;i++)
            {
                sk.push(i);
                if(sk.size()>cap){f=0; break;}
                while(!sk.empty() && sk.top()==v[j])
                {
                    sk.pop();
                    j++;
                }
            }
            if(sk.size()!=0) f=0;
            puts(f?"YES":"NO");
        }
    }
    return 0;
}
目录
相关文章
PAT (Advanced Level) Practice - 1057 Stack(30 分)
PAT (Advanced Level) Practice - 1057 Stack(30 分)
110 0
PAT (Advanced Level) Practice - 1057 Stack(30 分)
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
140 0
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
106 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
134 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
121 0
|
存储 测试技术
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
PAT (Advanced Level) Practice - 1131 Subway Map(30 分)
115 0
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
PAT (Advanced Level) Practice - 1068 Find More Coins(30 分)
117 0
PAT (Advanced Level) Practice - 1053 Path of Equal Weight(30 分)
PAT (Advanced Level) Practice - 1053 Path of Equal Weight(30 分)
130 0
PAT (Advanced Level) Practice - 1012 The Best Rank(25 分)
PAT (Advanced Level) Practice - 1012 The Best Rank(25 分)
120 0
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
131 0