Rails UVA - 514

版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/84779675
#include <iostream>
#include <stack>
#include <cstdio>

using namespace std;

const int N = 1000;

int target[N];
stack<int> s;
int n;

int Judge()
{
    int head = 0, num = 1;
    while(head < n) {
        if(num == target[head])
            num++, head++;
        else if(!s.empty() && s.top() == target[head]) {
            s.pop();
            head++;
        }
        else if(num <= n)
            s.push(num++);
        else
            return 0;
    }
    return 1;

}

int main()
{
    while(~scanf("%d", &n) && n) {
        while(1){
            while (!s.empty())
                s.pop();
            scanf("%d", &target[0]);

            if (target[0] == 0)
                break;
            for (int i = 1; i < n; i++)
                scanf("%d", &target[i]);

            printf("%s\n", Judge()? "Yes" : "No");
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/KLFTESPACE/article/details/84779675