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. poj 1221 UNIMODAL PALINDROMIC DECOMPOSITIONS (母函数)

    /* 给出一个数n,把它拆分成若干个数的和,要求最大的数在中间并向两边非递增.问拆法有多少种. 母函数.枚举中间的那一个数.由于左右对称.所以仅仅须要求左边部分的方案就可以. 注意,左右两部分的取数必 ...

  2. Java PreparedStatement

    PreparedStatement是一个用于运行sql语句的标准接口的对象.它是继承与Statement.依据里氏代换原则.用Statement运行的语句,一定能够用Prepared替换了.那么他们之 ...

  3. java文件创建、删除、读取、写入操作大全

    一.获得控制台用户输入的信息 public String getInputMessage() throws IOException...{ System.out.println("请输入您的 ...

  4. TCP closing a connection

    client closes socket: clientSocket.close(); step1 :client sends TCP FIN control segment to server st ...

  5. Macosx Setdns

    通过C语言接口在Mac App内部对系统的DNS配置进行改动. Mac OS X设置DNS代码 演示样例代码setDNS.c内容例如以下: #include <SystemConfigurati ...

  6. 【JAVA得知】struts2 于 Actionsupport 任务

    尊重原创:http://xumiao900.iteye.com/blog/469760     Action 跟 Actionsupport 的差别      当我们在写action的时候,能够实现A ...

  7. php查找字符串是否存在

    strstr //搜索字符串在另一字符串中的首次出现(对大小写敏感) //该函数返回字符串的其余部分(从匹配点).如未找到则返回 false stristr //查找字符串在另一字符串中第一次出现的位 ...

  8. [转载]起动service保存android系统log( logcat服务)

    原文链接:http://www.myexception.cn/android/1904013.html 启动service保存android系统log 作为android开发工程师,出现了BUG是否苦 ...

  9. linux 经常使用配置

    教研室用的非常旧的fedora14,装一些软件和下载东西的时候比較蛋疼,恰巧ubuntu14.04 公布,于是安装试试,顺便记录下经常使用的配置,备忘. 1. 制作镜像,比較老的主板,写入方式选择US ...

  10. 前端构建工具gulp

    前端构建工具gulp使用   前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gul ...