Rails 

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible
to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.



 

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has coaches
numbered in increasing order . The chief for train reorganizations must know whether it is possible to marshal coaches continuing
in the direction B so that their order will be . Help him and write a program that decides whether it is possible to
get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there
can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.

Input

The input file consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described
above. In each of the next lines of the block there is a permutation of The
last line of the block contains just 0.

The last block consists of just one line containing 0.

Output

The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains Yes if
it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains No. In addition, there is one empty line after
the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last ``null'' block of the input file.

Sample Input

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

Sample Output

Yes
No Yes

有n辆火车  按1到n的顺序进站  最后进站的车能够在不论什么时候出去  推断给定的出站序列是否可能

火车仅仅有两种状态  从A进站  或者从站到B   模拟栈的操作即可了

令A表示A中当前待进站的第一辆火车  tar[B]表示出站序列中当前应该出站的火车   sta为火车站

当A==tar[B]的时候  A进站立即出战   否则当站中最后一辆==tar[B]时   这辆车出站  都不满足就仅仅能A中的最前面的火车进站

当n辆火车所有进站  而站中还有火车是   给定的出战序列就是不可能的

#include<cstdio>
#include<stack>
using namespace std;
const int N = 1005;
int n, tar[N], A, B;
int main()
{
while (scanf ("%d", &n), n)
{
while (scanf ("%d", &tar[1]), tar[1])
{
for (int i = 2; i <= n; ++i)
scanf ("%d", &tar[i]);
stack<int> sta;
A = B = 1;
bool ok = true;
while (B <= n)
{
if (A == tar[B])
{ ++A; ++B; }
else if (!sta.empty() && sta.top() == tar[B])
{ sta.pop(); ++B; }
else if (A <= n)
sta.push (A++);
else
{ ok = false; break; }
}
printf (ok ? "Yes\n" : "No\n");
}
printf("\n");
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

UVa 514 Rails(经典栈)的更多相关文章

  1. UVa 514 Rails(栈的应用)

    题目链接: https://cn.vjudge.net/problem/UVA-514 /* 问题 输入猜测出栈顺序,如果可能输出Yes,否则输出No 解题思路 貌似没有直接可以判定的方法,紫书上给出 ...

  2. UVA ~ 514 ~ Rails (栈)

    参考:https://blog.csdn.net/ZscDst/article/details/80266639 #include <iostream> #include <cstd ...

  3. UVA - 514 Rails(栈模拟)

    题目: 给出一个序列,问将1,2,3,4……按从小到大的顺序入栈,能否得到给出的序列. 思路: 用stack模拟就可以了. 当前的cnt如果小于a[i],就将cnt入栈,否则就判断栈顶是不是和a[i] ...

  4. UVA 514 - Rails ( 铁轨)

    from my CSDN: https://blog.csdn.net/su_cicada/article/details/86939523 例题6-2 铁轨(Rails, ACM/ICPC CERC ...

  5. Uva - 514 - Rails

    C是一个栈,每次先检查A的第一个元素是否满足,如果满足,直接进入B:再检查C中栈顶元素是否满足,如果满足,出栈进入B:前两步都不满足将A放入C栈中.循环到B满或者A,C中都不满足条件并且A空,第一种情 ...

  6. 铁轨(rails, ACM/ICPC CERC 1997,Uva 514)

    铁轨(rails, ACM/ICPC CERC 1997,Uva 514) 题目描述 某城市有一个火车站,铁轨铺设如图所示.有n节车厢从A方向驶入车站,按进站顺序编号为1~n.你的任务是让它们按照某种 ...

  7. 【紫书】Rails UVA - 514 栈

    题意:判断出栈顺序是否合法 题解:两个指针,A指向入栈序列,B指向出栈. 的分三种情况:if     1.A==B :直接入栈加出栈即可A++,B++ else 2.和栈顶相同,直接出栈A==stac ...

  8. Rails UVA - 514(栈)

    题目链接:https://vjudge.net/problem/UVA-514 题目大意:右边的火车经过中间的收费站到左边,右边火车进站的秩序是1~n   判断是否能以题中是所给的次序通过 思路:很明 ...

  9. Rails,uva 514

    题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例 ...

随机推荐

  1. BT渗透工具使用学习笔记

    BT51.信息收集2.扫描工具3.漏洞发现4.社会工程学工具5.运用层攻击MSF6.局域网攻击7.密码破解8.维持访问一.DNS信息收集1.Dnsenum/pentest/enumeration/dn ...

  2. GOJ1150(矩阵快速幂)

    sum Time Limit: 1000ms Problem Description: 给定a和n,计算a+aa+aaa+aaaa+...+a...a(n个a) 的和. Input: 测试数据有多组, ...

  3. zoj1610(线段树)

    题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 题意:在0-8000长的线段里面,按先后次序依次覆盖颜色, ...

  4. Get与Post的差别

    Http定义了与server交互的不同方法,最主要的方法有4种,各自是GET,POST.PUT,DELETE. URL全称是资源描写叙述符.我们能够这样觉得:一个URL地址,它用于描写叙述一个网络上的 ...

  5. Learning Cocos2d-x for WP8(1)——创建首个项目

    原文:Learning Cocos2d-x for WP8(1)--创建首个项目 Cocos2d-x for WP8开发语言是C++,系列文章将参考兄弟篇Learning Cocos2d-x for ...

  6. ecshop 浏览历史样式的修改

    ecshop的浏览历史的样式,例如我修改的是只让浏览历史显示浏览历史的商品名称 而不显示浏览历史的商品的价格和图片 首先找到要修改 的文件includes\lib_insert.php 找到函数fun ...

  7. M3U8格式解说及实际应用分析

    M3U8有啥优点 ? 网上搜索了一下,大家众说纷纭,个人理解主要是能够做多码率的适配,依据网络带宽,client会选择一个适合自己码率的文件进行播放,保证视频流的流畅. 在IOS device和mac ...

  8. Android Handler Message总结一下

    当应用程序启动时,会开启一个主线程(也就是UI线程).由她来管理UI.监听用户点击.来响应用户并分发事件等.所以一般在主线程中不要运行比較耗时的操作,如联网下载数据等,否则出现ANR错误.所以就将这些 ...

  9. UML相关工具一览

    http://www.cnblogs.com/chehaoj/p/3478003.html TopCoder UML Tool 1.2.6 TopCoder, Inc http://www.topco ...

  10. Solr入门指南

    本文转自http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!499.entry?wa=wsignin1.0 因为搜索引擎功能 ...