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
题解:火车进栈;
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ");
typedef long long LL;
const int MAXN=1010;
int a[MAXN];
int N;
bool js(){
stack<int>S;
int i,j;
for(i=1,j=0;i<=N;i++){
S.push(i);
if(i==a[j]){
while(!S.empty()){
if(S.top()==a[j]){
j++;S.pop();
}
else break;
}
}
}
while(!S.empty()){
if(S.top()!=a[j])return false;
S.pop();j++;
}
return true;
}
int main(){
while(SI(N),N){
while(true){
SI(a[0]);
if(!a[0])break;
for(int i=1;i<N;i++)SI(a[i]);
if(js())puts("Yes");
else puts("No");
}
puts("");
}
return 0;
}
UVA-514 Rails (栈)的更多相关文章
- UVA 514 - Rails ( 铁轨)
from my CSDN: https://blog.csdn.net/su_cicada/article/details/86939523 例题6-2 铁轨(Rails, ACM/ICPC CERC ...
- UVa 514 Rails(经典栈)
Rails There is a famous railway station in PopPush City. Country there is incredibly hilly. The st ...
- 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 ...
- Rails UVA - 514(栈)
题目链接:https://vjudge.net/problem/UVA-514 题目大意:右边的火车经过中间的收费站到左边,右边火车进站的秩序是1~n 判断是否能以题中是所给的次序通过 思路:很明 ...
- UVA - 514 Rails(栈模拟)
题目: 给出一个序列,问将1,2,3,4……按从小到大的顺序入栈,能否得到给出的序列. 思路: 用stack模拟就可以了. 当前的cnt如果小于a[i],就将cnt入栈,否则就判断栈顶是不是和a[i] ...
- 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
题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例 ...
随机推荐
- js对JSON数据排序
一.适用于数字排序和字幕排序json 的排序方法有很多种,这是其中最简单的一种方法. 代码如下: var sortBy = function (filed, rev, primer) { rev ...
- Android应用程序架构之res
res/drawable 专门存放png.jpg等图标文件.在代码中使用getResources().getDrawable(resourceId)获取该目录下的资源. res/layout 专门存放 ...
- nginx 目录密码保护的设置方法
在 nginx.conf 文件中对应的 server 段中 添加 location ^~ /test/ { auth_basic TEST-Login; auth_basic_user_file /r ...
- eclipse同步远程服务器
eclipse里有一个强大的插件,可以直接在本地编辑远程服务器代码,Eclipse Remote System Explorer (RSE) 下载安装方法: 一.下载,高版本的eclipse可以直接下 ...
- PICT安装及使用
一:PICT安装 1.下载pict33.msi:http://vdisk.weibo.com/s/d6k2tcgXDa7Eq 2.安装: 二:PICT的使用 1.在F:\PICT 目录下,新建一个tx ...
- MSI文件静默安装
以.net4为例,以下命令为静默安装: dotNetFx40_Full_x86_x64.exe /q /norestart /ChainingPackage FullX64Bootstrapper / ...
- B2C 电商网站需要怎样的 ERP 系统
B2C 电商网站需要怎样的 ERP 系统 主要由如下一些系统组成:1.进销存系统,你的产品的采供销当然最好是由系统来实现:2.BI系统,BI包括所有的流量.订单.商品.库存.发货等所有数据节点,亦包含 ...
- Oracle提示“资源正忙,需指定nowait”的解决方案
Oracle提示“资源正忙,需指定nowait”的解决方案 | T 本文我们主要介绍了Oracle数据库操作表时提示“资源正忙,需指定nowait”的解决方案,希望能够对您有所帮助. AD:51CT ...
- logrotate 日志清理后 rsyslog中断问题
<pre name="code" class="html">随后配置logrotate的配置文件/etc/logrotate.conf,加入下面的内 ...
- 运行预构建 Linux 映像的 Windows Azure 虚拟机中的交换空间 – 第 1 部分
本文章由 Azure CAT 团队的 Piyush Ranjan (MSFT) 撰写. 随着基础结构服务(虚拟机和虚拟网络)近期在 Windows Azure 上正式发布,越来越多的企业工作负荷正在向 ...