铁轨

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
const int MAXN = 1000 + 10;
int n, target[MAXN];
int main()
{
    while(scanf_s("%d",&n,sizeof(n)) == 1)
    {
        int stack[MAXN],top = 0;
        int A = 1, B = 1;
        for(int i = 1;i < n;i++)
        {
            scanf_s("%d",&target[i],sizeof(target[i]));
        }
        int ok = 1;
        while(B <= n)
        {
            if(A == target[B])
            {
                A++;
                B++;
            }
            else 
                if(top && stack[top] == target[B])
                { 
                    top--;
                    B++;
                }
            else
                if(A <= n)
                    stack[++top] = A++;
                else
                {
                    ok = 0;
                    break;
                }
                printf_s("%s\n",ok?"Yes":"No");
        }
        system("pause");
        return 0;
    }
}

 

posted @ 2015-04-17 09:41  码农@163  阅读(141)  评论(0编辑  收藏  举报