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. VMware虚拟机直连物理网络的两种方式

    VMware虚拟机直连物理网络的两种方式   使用VMware构建虚拟机,通常虚拟机都使用NAT模式.这时,虚拟机有独立的网段.使用NAT模式,虚拟机之间数据都通过虚拟网络传输,不会影响实体机所在的实 ...

  2. git alias

    alias|grep git g=git ga='git add' gaa='git add --all' gapa='git add --patch' gb='git branch' gba='gi ...

  3. 1.搭建maven,eclipse创建maven项目

    1.下载maven包,下载地址为:http://maven.apache.org/download.cgi 2.解压zip包 3.eclipse 引入maven: window-Preferences ...

  4. Ubuntu 16.04下安装WineHQ

    说明: 1.Wine和WIneHQ没什么区别,新版和旧版的问题. 2.安装了深度的Wine包也可以和WineHQ一起兼容,因为深度的应用名已经加了deepin前缀,所以不冲突. 3.安装了Wine之后 ...

  5. CheckedListBoxControl 或CheckedListBox 控件中显示水平滚动条 z

    public partial class Form1 : Form { public Form1() { InitializeComponent(); DisplayHScroll(); } /// ...

  6. HashMap 和 Hashtable 的同和不同

    综述 可以直接根据 hashcode 值判断两个对象是否相等吗?肯定是不可以的,因为不同的对象可能会生成相同的 hashcode 值.虽然不能根据 hashcode 值判断两个对象是否相等,但是可以直 ...

  7. Vue2.0 视频教程

    好像是一套vue 开发webapp 课程.来自网络. url:https://pan.baidu.com/s/1jIele9w password:b404 文章来源:刘俊涛的博客 地址:http:// ...

  8. 有关C/C++指针的经典面试题(转)

    参考一: 有关C/C++指针的经典面试题 0.预备知识,最基础的指针 其实最基础的指针也就应该如下面代码: int a; int* p=&a; 也就是说,声明了一个int变量a,然后声明一个i ...

  9. LINUX下目标文件的BSS段、数据段、代码段

    http://blog.chinaunix.net/uid-27018250-id-3867588.html bss 未初始化的全局数据 data 已经初始化的全局数据 text 代码段,机器指令 r ...

  10. jdk的动态代理源代码解析

    先看一下JDK的动态是怎么用的. package dynamic.proxy; import java.lang.reflect.InvocationHandler; import java.lang ...