PAT甲1051 Pop Sequence (25)(25 分)

#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
using namespace std;

int A[1010];

int main()
{
    int N,M,K;
    stack<int> S;
    scanf("%d%d%d",&M,&N,&K);
    for(int i=0;i<K;i++)
    {
        bool flag=true;
        for(int j=1;j<=N;j++)
        {
            scanf("%d",&A[j]);
        }
        int index=1,num=1;
        while(index<=N)
        {
            if(A[index]>=num)
            {
                S.push(num);
                num++;
            }
            else
            {
                int top=S.top();
                if(top!=A[index])
                {
                    flag=false;
                    break;
                }
                else
                {
                    index++;
                    S.pop();
                }
            }
            if(S.size()>M)
            {
                flag=false;
                break;
            }
        }
        if(flag)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
        while(!S.empty())
        {
            S.pop();
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yhy489275918/article/details/81841895