Rails
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 25964   Accepted: 10199

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 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 contains the lines corresponding to the lines with permutations
in the input. A line of the output contains Yes if it is possible to
marshal the coaches in the order required on the corresponding line of
the input. Otherwise it contains No. In addition, there is one empty
line after the lines corresponding to one block of the input. There is
no line in the output corresponding to the last ``null'' block of the
input.

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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <algorithm>
#include <stack> using namespace std; int main()
{
int n;
int i, j;
int a[1010];
stack<int>q; while(scanf("%d", &n) && n!=0 )
{
while(scanf("%d", &a[0]) && a[0]!=0 )
{
for(i=1; i<n; i++)
{
scanf("%d", &a[i] );
}
int A=1, B=0;
int flag=1;
while(!q.empty())
{
q.pop();
}
while( B<n )
{
if(A==a[B])
{
A++; B++;
}
else if(!q.empty() && q.top()==a[B] )
{
q.pop(); B++;
}
else if(A<=n)
q.push(A++);
else
{
flag=0; break;
}
}
printf("%s\n", flag==1?"Yes":"No" );
}
printf("\n");
}
return 0;
}

poj 1363 Rails (【栈的应用】 刘汝佳的写法 *学习)的更多相关文章

  1. POJ 1363 Rails(栈)

    思路:将出车站的顺序存入数组train,由于入车站的顺序是固定的,为1~N,所以用P表示进站的车,初始为1. 接下来举例说明吧: 原来入站顺序:    1 2 3 4 5 读入的出战顺序: 3 4 2 ...

  2. vector刘汝佳算法入门学习笔记

    //*****-*-----vector***/////// 常用操作封装,a.size();可以读取大小               a.resize();可以改变大小:               ...

  3. ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]

    原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...

  4. c++20701除法(刘汝佳1、2册第七章,暴搜解决)

    20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述     输入正整数n,按从小到大的顺序输出所有 ...

  5. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 1(String)

    第一题:401 - Palindromes UVA : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8 ...

  6. 刘汝佳黑书 pku等oj题目

    原文地址:刘汝佳黑书 pku等oj题目[转]作者:小博博Mr 一.动态规划参考资料:刘汝佳<算法艺术与信息学竞赛><算法导论> 推荐题目:http://acm.pku.edu. ...

  7. [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总

    本文出自   http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner  打开 这个专题一共有25题,刷完 ...

  8. POJ 1363 Rails(栈)

    题目代号:POJ 1363 题目链接:http://poj.org/problem?id=1363 题目原题: Rails Time Limit: 1000MS   Memory Limit: 100 ...

  9. poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】

    问题如下: 问题 B: Rails 时间限制: Sec 内存限制: MB 提交: 解决: [提交][状态][讨论版] 题目描述 There is a famous railway station in ...

随机推荐

  1. BZOJ 4568 [Scoi2016]幸运数字(树链剖分 + 异或线性基)

    题目链接  BZOJ 4568 考虑树链剖分+线段树维护每一段区域的异或线性基 对于每个询问,求出该点集的异或线性基.然后求一下这个线性基里面能异或出的最大值即可. #include <bits ...

  2. Widows下利用OpenSSL生成证书

    1.下载OpenSSL的windows版本 32位:openssl-1.0.2a-i386-win32.zip 64位:openssl-1.0.2a-x64_86-win64.zip 下载之后解压即可 ...

  3. IntelliJ IDEA版本:Ultimate、Community、EAP版本的区别

    Community: 社区版,免费,但是功能有限制,Android Studio就是基于这个版本定制的. http://idea-intellij.com/intellij-community/ Ul ...

  4. 为VLC增加在线字幕插件VLSub

    VLC的在在线字幕插件VLSub,官网:https://github.com/exebetche/vlsub. 原理是通过搜索全球最大的字幕网站https://www.opensubtitles.or ...

  5. Maven自动部署war到Tomcat8

    原文:http://www.cnblogs.com/yucongblog/p/5392932.html 我使用的环境是:Eclipse Java EE IDE for Web Developers(V ...

  6. iOS开发 解决使用AVAudioRecorder录制后转mp3解决音量小的问题

    使用AVAudioRecorder录音后使用avplayer播放声音小,录音完后转成mp3格式的音频声音也小!!! 老板要求最基本的是不用把手机放到耳边听! 在StackOverFlow上查了一下,加 ...

  7. python 工具 二进制文件处理之——去掉指定长度数据包头

    包头48bit 数据98464 ...如此循环: piece_size = 48 piece_size1 = 98464 with open("C:\\Users\\Administrato ...

  8. Resolving 'Root Partition Is Filling Up' Issue on Sophos UTM Firewall

    from: https://wandersick.blogspot.com/2016/06/resolving-root-partition-is-filling-up.html This is a ...

  9. mysql 导入sql文件时自动切换了大小写

    windows环境下: 解决办法(即将其改为大小写敏感): 在my.ini中添加 lower_case_table_names=1

  10. requests(爬虫常用)库的使用

    Requests库的使用 基于urllib改写的库 示例: import requests response=requests.get('http://www.baidu.com')#get请求 pr ...