[UVa514] Rails

难点:

1. 要可以从题目中想到运用stack 然而这一点是十分明显的:进入中转站之后先进后出

2. UVa读入需要动脑筋

盲点:

1. current++代表着车已出站 若进入中转站则不能current++

2. 不需要再定义一个a数组来存1,2,...,n 因为可以直接和i比较

今日附上程序以便日后对照:

/*
    Source: UVa514
    Status: AC
    Done: 19/11/2015
    Remarks: stack
*/
#include<cstdio>
using namespace std;
int b[1005],c[1005];
int main()
{
    int top,n,i,cur;
    while(1){
        scanf("%d",&n);
        if(n==0)break;
        while(1){
            top=0;
            scanf("%d",&b[1]);
            if(b[1]==0){
                printf("
");
                break;
            }
            for(i=2;i<=n;i++){
            scanf("%d",&b[i]);
            }
            cur=1;
            top=0;
            for(i=1;i<=n;i++){
                if(i!=b[cur]){
                    c[++top]=i;//insert elements in the stack
                       //do not write i as b[cur] A起始站的车进入中转站
                   }
                   else cur++; //A起始站的车可以直接出站
                while(top && c[top] == b[cur])//stack is not empty c中转站中的车可以出站
                {
                    top--;//delete elements in the stack
                    cur++;
                }
            }
            if(!top) printf("Yes
");
            else     printf("No
");
        }
    } 
    return 0;
}
原文地址:https://www.cnblogs.com/peccavi/p/4978718.html