E - Rails (栈)
E - Rails
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
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 N ≤ 1000 coaches numbered in increasing order 1, 2, . . . , N. 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 a1.a2, . . . , aN . 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 1, 2, . . . , N. 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
//用栈保存进入c的火车车厢,A指向没还没进去的,B指示的是目标的火车位置
#include <iostream>
#include <stack>
using namespace std; int main()
{
int t,flag;
int target[]; while (cin>>t&&t)
{
stack<int> s;//进入c车站的车厢
while ()
{
flag=;
for (int i=;i<=t;i++)
{
cin>>target[i];
if (target[]==)
{
flag=;
break;
}
} if (flag==)
{
putchar();
break;
} int A=,B=;//
int ok=;
while (B<=t)
{
if (A<=t&&A==target[B]){A++;B++;}
else if (!s.empty()&&s.top()==target[B]){ B++; s.pop(); }
else if (A<=t){ s.push(A); A++; }
else { ok=; break; }
}
if (ok==) cout<<"Yes\n";
else if(ok==) cout<<"No\n";
}
}
return ;
}
E - Rails (栈)的更多相关文章
- POJ 1363 Rails(栈)
思路:将出车站的顺序存入数组train,由于入车站的顺序是固定的,为1~N,所以用P表示进站的车,初始为1. 接下来举例说明吧: 原来入站顺序: 1 2 3 4 5 读入的出战顺序: 3 4 2 ...
- UVA-514 Rails (栈)
Rails There is a famous railway station in PopPush City. Country there is incredibly hilly. The s ...
- UVa514 Rails (栈)
题意:一列有n节车厢的火车按顺序进站,给你一个出站顺序,问你该火车的车厢能否以该顺序出站? 分析:出站的车厢满足后进先出的关系,所以我们考虑采用栈s 假设车厢一共有n节,n = 5: 进站顺序A:1 ...
- Rails 5 Test Prescriptions 第3章Test-Driven Rails
本章,你将扩大你的模型测试,测试整个Rails栈的逻辑(从请求到回复,使用端到端测试). 使用Capybara来帮助写end-to-end 测试. 好的测试风格,包括端到端测试,大量目标明确的单元测试 ...
- poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】
问题如下: 问题 B: Rails 时间限制: Sec 内存限制: MB 提交: 解决: [提交][状态][讨论版] 题目描述 There is a famous railway station in ...
- UVa 514 Rails(经典栈)
Rails There is a famous railway station in PopPush City. Country there is incredibly hilly. The st ...
- 从 rails 窥探 web 全栈开发(零)
从 rails 窥探 web 全栈开发(零) 本文将讲述在学习之前几个必须要知道的概念,这些词汇在 rails 中都会出现. 本文前置条件:安装好 Ruby. 从 rails 窥探 web 全栈开发( ...
- UVa 514 Rails(栈的应用)
题目链接: https://cn.vjudge.net/problem/UVA-514 /* 问题 输入猜测出栈顺序,如果可能输出Yes,否则输出No 解题思路 貌似没有直接可以判定的方法,紫书上给出 ...
- 【紫书】Rails UVA - 514 栈
题意:判断出栈顺序是否合法 题解:两个指针,A指向入栈序列,B指向出栈. 的分三种情况:if 1.A==B :直接入栈加出栈即可A++,B++ else 2.和栈顶相同,直接出栈A==stac ...
随机推荐
- php中一些函数的用法
addslashes() 定义和用法 addslashes() 函数返回在预定义字符之前添加反斜杠的字符串. 预定义字符是: 单引号(') 双引号(") 反斜杠(\) NULL 提示:该函数 ...
- Linux经常使用命令(十五) - which
我们常常在linux要查找某个文件,但不知道放在哪里了.能够使用以下的一些命令来搜索: which 查看可运行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置 ...
- flask 框架 前端和后端请求超时问题
部署模式 flask + Gunicorn + nginx 为什么要用Gunicorn + nginx ? 请看知乎大神们的回答:https://www.zhihu.com/question/3852 ...
- 9、Linux驱动的杂项设备
杂项设备,是字符设备中的特殊,它的主设备号,是 10,不同的杂项设备,通过次设备号进行区分. 1.注册与注销 int misc_register(struct miscdevice * misc) 完 ...
- hiredis中异步的实现小结
hiredis中异步的实现小结 原文: http://blog.csdn.net/l1902090/article/details/3858... 时间: 2014-08-15 前言 一般情况下我们使 ...
- Android Camera 使用小结。两种方法:一是调用系统camera app,二是自己写camera程序。
源文链接:http://www.cnblogs.com/franksunny/archive/2011/11/17/2252926.html Android Camera 使用小结 Android手机 ...
- 牛散NO.2:MACD西施说风情,柳下惠高位勿迷情
创业板日线“高位夺命勾魂枪” 话说在创业板的波段调整中,MACD的勾魂枪同样让多头“魂断蓝桥”.圈内图形又好比西施姑娘回眸一笑,吴王夫差便注定命赴黄泉了.范蠡的精心设计让西施 布了一个风情万种的局,被 ...
- CSS实现子级窗口高度随低级窗口高度变化及js控制左右容器高度一致
纯粹使用使用height:100%;或者height:auto;来定义内部容器自适应高度,都无法实现让内部容器高度随着外部父容器高度变化而变化,所以我们必需要使用position绝对定位属性来配合协助 ...
- Atitit.异常机制的设计原理
Atitit.异常机制的设计原理 缺陷 关键是只要知晓有一个异常表的存在,try 的范围就是体现在异常表行记录的起点和终点.JVM 在 try 住的代码区间内如有异常抛出的话,就会在当前栈桢的异常表中 ...
- 下载文件,ie文件名称乱码问题
设置响应编码,将文件名称用java.net.URLEncoder.encode编码,这样就不会乱码了 java.net.URLEncoder.encode response.setCharacterE ...