PAT (Advanced Level) 1051. Pop Sequence (25)

简单题。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<stack>
#include<vector>
using namespace std;

int n,k,m;
int a[1000+10];
stack<int>S;

bool check()
{
    while(!S.empty()) S.pop();
    int Max=0,sz=0;
    int now=1;
    int p=1;
    while(1)
    {
        int tmp=sz;
        if(S.empty()&&p<=k)
        {
            S.push(p++);
            sz++;
            Max=max(sz,Max);
        }
        else
        {
            if(S.top()==a[now])
            {
                S.pop(); now++;
                sz--;
            }
            else
            {
                if(p<=k)
                {
                    S.push(p++);
                    sz++;
                    Max=max(sz,Max);
                }
            }
        }
        if(now==k+1||sz==tmp) break;
    }
    if(Max<=m&&now==k+1) return 1;
    return 0;
}

int main()
{
    scanf("%d%d%d",&m,&k,&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=k;j++) scanf("%d",&a[j]);
        if(check()) printf("YES
");
        else printf("NO
");
    }

    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5527624.html