UVa 514 Rails(经典栈)
| 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(经典栈)的更多相关文章
- UVa 514 Rails(栈的应用)
题目链接: https://cn.vjudge.net/problem/UVA-514 /* 问题 输入猜测出栈顺序,如果可能输出Yes,否则输出No 解题思路 貌似没有直接可以判定的方法,紫书上给出 ...
- UVA ~ 514 ~ Rails (栈)
参考:https://blog.csdn.net/ZscDst/article/details/80266639 #include <iostream> #include <cstd ...
- UVA - 514 Rails(栈模拟)
题目: 给出一个序列,问将1,2,3,4……按从小到大的顺序入栈,能否得到给出的序列. 思路: 用stack模拟就可以了. 当前的cnt如果小于a[i],就将cnt入栈,否则就判断栈顶是不是和a[i] ...
- UVA 514 - Rails ( 铁轨)
from my CSDN: https://blog.csdn.net/su_cicada/article/details/86939523 例题6-2 铁轨(Rails, ACM/ICPC CERC ...
- Uva - 514 - Rails
C是一个栈,每次先检查A的第一个元素是否满足,如果满足,直接进入B:再检查C中栈顶元素是否满足,如果满足,出栈进入B:前两步都不满足将A放入C栈中.循环到B满或者A,C中都不满足条件并且A空,第一种情 ...
- 铁轨(rails, ACM/ICPC CERC 1997,Uva 514)
铁轨(rails, ACM/ICPC CERC 1997,Uva 514) 题目描述 某城市有一个火车站,铁轨铺设如图所示.有n节车厢从A方向驶入车站,按进站顺序编号为1~n.你的任务是让它们按照某种 ...
- 【紫书】Rails UVA - 514 栈
题意:判断出栈顺序是否合法 题解:两个指针,A指向入栈序列,B指向出栈. 的分三种情况:if 1.A==B :直接入栈加出栈即可A++,B++ else 2.和栈顶相同,直接出栈A==stac ...
- Rails UVA - 514(栈)
题目链接:https://vjudge.net/problem/UVA-514 题目大意:右边的火车经过中间的收费站到左边,右边火车进站的秩序是1~n 判断是否能以题中是所给的次序通过 思路:很明 ...
- Rails,uva 514
题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例 ...
随机推荐
- c++一些语法模板
函数模板特 template <class T> int compare(T v1,T v2) { if(v1<v2) return -1; else if(v1>v2) re ...
- hdu1535(最短路)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1535 题意:给你一个源点,让你从这里派发n个学生去其余的n-1个站点去邀请人们去CSS,然后再返回CS ...
- 【Energy Big Data】能源互联网和电力大数据
背景 今年的政府工作报告突出了互联网在经济结构转型中的重要地位,报告明白指出:要制定"互联网+"行动计划,推动移动互联网.云计算.大数据.物联网等与现代制造业结合,促进电子商务.工 ...
- ServiceCallSite
ServiceCallSite 通过上一篇的介绍我们应该对实现在ServiceProvider的总体设计有了一个大致的了解,但是我们刻意回避一个重要的话题,即服务实例最终究竟是采用何种方式提供出来的. ...
- 使用MSPT实现二层冗余
- java 短信验证码===随机数
生成验证码,验证码生成 String mobile = phone;// 手机号码,多个号码使用","分割 // 生成随机6位码 String s = ""; ...
- c/c++中main函数参数讲解
参考地址: http://blog.csdn.net/cnctloveyu/article/details/3905720 我们经常用的main函数都是不带参数的.因此main 后的括号都是空括号.实 ...
- malloc一次性最大能申请多大内存空间
受用户态内存地址空间的限制.64 位系统下分配几个 T 不成问题. 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:zz matrix链接:http://www.zhihu. ...
- WPF换肤之八:创建3D浏览效果
原文:WPF换肤之八:创建3D浏览效果 上节中,我们展示了WPF中的异步以及界面线程交互的方式,使得应用程序的显示更加的流畅.这节我们主要讲解如何设计一个具有3D浏览效果的天气信息浏览器. 效果显示 ...
- Oracle ORA-01034,ORA-27101,ORA-00600
本机IP地址:192.168.1.163 [oracle@rtest ~]$ sqlplus /nolog SQL*Plus: Release 10.2.0.2.0 - Production on S ...