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方向的铁轨并驶入车站.例 ...
随机推荐
- UVA 10140 - Prime Distance(数论)
10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...
- java与c/c++进行socket通信
比如Server端只接收一个结构Employee,定义如下: struct UserInfo { char UserName[20]; int UserId; }; struct Employ ...
- 第6本:《The Hunger Games》
第6本:<The Hunger Games> 以前从未读过一本完整的英文小说,所有就在今年的读书目标中增加了一本英文小说,但在 头四个月内一直没有下定决定读哪一本.一次偶然从SUN的QQ空 ...
- 嵌入式Linux下BOA网页server的移植
**************************************************************************************************** ...
- Java如何检查List<String> 里是否有想要的字符串?
List<String> test = new ArrayList<String>(); test.add("a"); test.add("b&q ...
- httpcomponents-client-ga(4.5)
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/ Chapter 1. Fundamentals Prev Next ...
- Redshift扩容及踩到的坑
下午发现redshift集群已经没有什么空间了.删掉一些不须要的暂时表也仅仅降到86%左右,为了能放下这两天的数据必须扩容了 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...
- iosclient暑期“动画屋“活动项目总结
入职实习的这个公司,第一天就分配了任务.从零開始写一个网页.之前尽管了解一些前端知识.但从头开写还是遇到了非常多问题,互联网公司讲求效率,有deadline还是比較有紧迫感的,与在实验室放羊状 ...
- POJ 2536 Gopher II(二分图的最大匹配)
题目链接:http://poj.org/problem?id=2536 题意:已知有n仅仅老鼠的坐标,m个洞的坐标,老鼠的移动速度为V,S秒以后有一仅仅老鹰要吃老鼠,问有多少个老鼠被吃. 非常明晰,二 ...
- windows phone (18) Border元素
原文:windows phone (18) Border元素 Border类是对某一个对象的周围边框,背景,或者同时绘制两者,首先看一个简单的例子进行分析[作者:神舟龍] xaml文件: <!- ...