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 先输入火车车厢数N,然后输入N个数代表车厢编号a[i],判断火车是否能按1~N或者N~1的顺序开出,a[0] = 0;代表车厢数为N的循环结束,N = 0;代表程序结束。
思路:    
先输入车厢编号,用数组a[i]保存,在从1~N依次输入栈st中,依次判断st.top()是否等于[i].
 #include<cstdio>
#include<stack>
using namespace std;
int main()
{
int n,a[],i,j;
while(scanf("%d",&n) && n)
{
stack<int>st;
while(scanf("%d",&a[]) && a[])
{
for( i = ; i < n ; i++)
{
scanf("%d",&a[i]);
}
for( i = , j = ; i <= n ; i++)
{
st.push(i);
while(st.top() == a[j])
{
j++;
st.pop();
if(st.empty()) //栈为空时结束循环
break;
}
}
if( j == n) printf("Yes\n");
else
printf("No\n");
}
printf("\n");
}
}

poj 1363 火车进站 (栈的应用)的更多相关文章

  1. POJ 1363 Rails(栈)

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

  2. poj 1363 Rails (【栈的应用】 刘汝佳的写法 *学习)

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

  3. 华为OJ:火车进站

    火车进站 给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号.要求以字典序排序输出火车出站的序列号. 输入描述: 有多组测试用例, ...

  4. C++ HOJ 火车进站

    [问题描写叙述] 给定一个正整数N代表火车数量.0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号. 要求以字典序排序输出火车出站的序列号. 输入:   有多组 ...

  5. (hdu)1022 Train Problem I 火车进站问题

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 Problem Description As the new term comes, ...

  6. 【火车出栈】ZOJ - 2603 Railroad Sort

    好久没写递归了,怕手生再来练练手. 题意:车轨上有上图所示的n个中转栈,现有2n个列车,给出列车初始编号序列.列车从最右边驶入车轨,并且列车只能从右向左移动,要求给出列车中转操作序列,使列车经过这n个 ...

  7. 【讲●解】火车进出栈类问题 & 卡特兰数应用

    火车进出栈类问题详讲 & 卡特兰数应用 引题:火车进出栈问题 [题目大意] 给定 \(1\)~\(N\) 这\(N\)个整数和一个大小无限的栈,每个数都要进栈并出栈一次.如果进栈的顺序为 \( ...

  8. CodeVS3958 火车进站

    3958 火车进站 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master         题目描述 Description 火车站内往往设有一些主干线分叉出去的铁路支路 ...

  9. luogu P1044 火车进出栈问题(Catalan数)

    Catalan数就是魔法 火车进出栈问题即: 一个栈(无穷大)的进栈序列为 1,2,3,4,...,n 求有多少个不同的出栈序列? 将问题进行抽象, 假设'+'代表进栈, 则有'-'代表出栈 那么如果 ...

随机推荐

  1. ACM经历与感悟合集

    ACM经历与感悟合集 ACM起步要点总结(转哈工大) ACM 荣耀之路-学习方法 ACM感悟 一位ACMer过来人的心得 ACM经历总结 大学ACM的总结 ACM大牛的退役贴 各大牛退役总结帖 女生应 ...

  2. [转]Keyword Reference (F#)

    Visual F# Development Portal http://msdn.microsoft.com/en-us/library/vstudio/ff730280.aspx 本文转自:http ...

  3. Java 线程是什么-渐入佳境

    线程:(一)什么是线程 管哥说:程序中有多个执行流就叫多线程.多线程是多任务的一种特别的形式.好处:单个程序可以创建多个并发执行的程序来完成各自的任务.多线程能满足程序员编写高效率的程序来达到充分利用 ...

  4. (六)SpringIoc之延时加载

    Spring容器初始化时将所有scope = singleton的bean进行实例化. 通常情况下这是一件好事,因为这样在配置中的错误会更容易发现.但是如果不想spring容器初始化就实例化就要用到延 ...

  5. 手写一套迷你版HTTP服务器

    本文主要介绍如何通过netty来手写一套简单版的HTTP服务器,同时将关于netty的许多细小知识点进行了串联,用于巩固和提升对于netty框架的掌握程度. 服务器运行效果 服务器支持对静态文件css ...

  6. 开源项目:JEECG

    工程下载:https://github.com/zymqqc/jeecg-1

  7. Android开发中使用startActivityForResult()方法从Activity A跳转Activity B出现B退出时A也同时退出的解决办法

    最近一个 App 中用到了 startActivityForResult() 方法,使用的时候却出现了一些问题,比如我在 Activity A 中调用该方法向 Activity B 中跳转,如果 B  ...

  8. Fragment懒加载预加载

    1. 预加载viewpager.setOffscreenPageLimit(2);,默认是预加载1,可以结合懒加载使用. 如果希望进入viewpager,Fragment只加载一次,再次滑动不需加载( ...

  9. struts2 <allowed-methods > 标签配置

    1.在struts2   2.5版本中添加了对方法访问的权限,如果没有被添加到<allow-method> 方法的标签,将会报一下错误 5:05:18.078 [http-apr-8020 ...

  10. KMS

    slmgr -ipk 73KQT-CD9G6-K7TQG-66MRP-CQ22C