码迷,mamicode.com
首页 > 其他好文 > 详细

Rails UVA-514 (stack)

时间:2020-02-07 19:02:20      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:lse   empty   itemid   net   while   lin   putchar   names   utc   

vjudge链接

原题链接

  • 题目大意

判断列车是否能以特定顺序离开车站,车厢最多1000辆。

  • 分析

可以将铁轨重叠部分看作一个栈。车厢陆续入栈,当栈顶的车厢号和需要出栈的车厢号相同时将该车厢出站。若所有车厢都已经进入却依然无法满足要求时,可以判断无法以该顺序离开车站。

/*
 *lang C++ 5.3.0
 *user Weilin_C
*/

#include <iostream>
#include <stack>

using namespace std;

int main()
{
    stack <int> ista;
    int n;
    int no[1005];

    while (scanf("%d", &n) && n) {
        while (scanf("%d", &no[0]) && no[0]) {
            for (int i=1; i<n; i++) scanf("%d", &no[i]);
            while (!ista.empty()) ista.pop();
            int outn=0, car=1;
            while (outn!=n && car<=n+1) {
                if (ista.empty() || ista.top()!=no[outn]) ista.push(car++);
                else {ista.pop(); outn++;}
            }
            if (outn==n) printf("Yes\n");
            else printf("No\n");
        }
        putchar('\n');
    }

    return 0;
}

by SDUST weilinfox
原文链接:https://www.cnblogs.com/weilinfox/p/12273783.html

Rails UVA-514 (stack)

标签:lse   empty   itemid   net   while   lin   putchar   names   utc   

原文地址:https://www.cnblogs.com/weilinfox/p/12273783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!