首页 > 其他 > 详细

[UVa514] Rails

时间:2015-11-19 20:42:08      阅读:240      评论:0      收藏:0      [点我收藏+]

难点:

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("\n");
                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
                       //cur++;
                   }
                   else cur++;
                while(top && c[top] == b[cur])//stack is not empty
                {
                    top--;//delete elements in the stack
                    cur++;
                }
            }
            if(!top) printf("Yes\n");
            else     printf("No\n");
        }
    } 
    return 0;
}

 

[UVa514] Rails

原文:http://www.cnblogs.com/peccavi/p/4978718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!