1.链接:

http://poj.org/problem?id=1363

http://bailian.openjudge.cn/practice/1363

2.题目:

Rails
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24612   Accepted: 9644

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

Source

3.思路:

4.代码:

 #include "stdio.h"
//#include "stdlib.h"
#include "stack"
using namespace std;
int main()
{
stack<int> s;
int n,i,j,tmp,top;
bool flag;
scanf("%d",&n);
while(n!=)
{
scanf("%d",&tmp);
while(tmp != )
{
flag = true;
//top = tmp-1;
while(!s.empty()) s.pop();
for(i = ;i < tmp;i++) s.push(i);
top = tmp;
for(i = ;i < n;i++)
{
scanf("%d",&tmp);
if(flag)
{
//if(top < tmp) top = tmp - 1;
if(top < tmp) {for(j = top+;j<tmp;j++){s.push(j);}top=tmp;}
//else if(top == tmp) top--;
else if(s.top() == tmp) s.pop();
else flag = false;
}
}
if(!flag) printf("No\n");
else printf("Yes\n");
scanf("%d",&tmp);
}
scanf("%d",&n);
while(!s.empty()) s.pop();
if(n != ) printf("\n");
} //system("pause");
return ;
}

OpenJudg / Poj 1363 Rails的更多相关文章

  1. POJ 1363 Rails(栈)

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

  2. poj 1363 Rails 解题报告

    题目链接:http://poj.org/problem?id=1363 题意:有一列火车,车厢编号为1-n,从A方向进站,向B方向出站.现在进站顺序确定,给出一个出站的顺序,判断出站顺序是否合理. 实 ...

  3. POJ 1363 Rails

    Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21728   Accepted: 8703 Descriptio ...

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

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

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

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

  6. POJ 1363 Rails(栈)

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

  7. poj 1363

    这是一道数据结构的问题,用到了栈的知识.题目大意讲的是每一次有N辆车从A到B,但是要将车辆的顺序重新排列,可以通过中转站C来辅助排列,但是C符合先进后出的原则,这一点和栈的特性相同. 整个重新排序的过 ...

  8. POJ 1036 Rails 模拟堆栈

    水题,主要是思路清晰,判断明确. 记x为A站最前方的车,y表示下一列要进入B站的车厢,初识时,x=1;y=a1;C=[]; 在调度过程中: if(y==0)那么调度成功,退出模拟过程:否则 if(x= ...

  9. 【栈】 poj 1363

    poj1363,这道题是数据结构与算法中的经典问题,给定一组进栈顺序,问栈有多少种出去的顺序. #include<stdio.h> #include <stack> #incl ...

随机推荐

  1. ios开发 数据库版本迁移手动更新迭代和自动更新迭代

    数据库版本迁移顾名思义就是在原有的数据库中更新数据库,数据库中的数据保持不变对表的增.删.该.查. 数据持久化存储: plist文件(属性列表) preference(偏好设置) NSKeyedArc ...

  2. 让Visual Studio 2015 支持ASP.NET MVC4.0.0.1

    近日装上了Visual Studio 2015 ,打开之前vs2013创建的MVC4的项目发现无法编译通过,提示System.Web.MVC,System.Web.WebPages 等找不到,网上搜索 ...

  3. innobackupex --slave-info参数的含义和适用场景

    http://blog.itpub.net/28916011/viewspace-1969135/       我有个问题一直没弄明白,就是innobackupex里面的--slave-info这个参 ...

  4. 常用免费快递查询API对接案例

    现在许多电商公司和ERP都会寻找比较适用的集成快递查询接口,减少对接难度,现在整理一下常用的免费快递查询接口,并附上调用案例,如果有觉得不对的地方,望能够一起沟通探讨! 一.快递查询接口 目前有提供免 ...

  5. Android(java)学习笔记136:Java类初始化顺序

    Java类中初试化的顺序: 由此得出Java普通类初始化顺序结论: 静态变量 静态初始化块 变量 初始化块 构造器 由此得出Java继承类初始化顺序结论:     1 .继承体系的所有静态成员初始化( ...

  6. c++内联函数与静态函数

    不能是虚函数的成员函数有:静态成员函数,内联成员函数,构造函数.没有什么函数需要硬性规定为虚函数,一般析构函数会被定义为虚函数其他就是在继承类中可能需要override的类成员函数应该定义为虚函数 h ...

  7. typeid关键字

    这么看下去太要命了,有太多东西要学了... 而且视频看起来的确费神,费脑,费耳朵. 所以决定由视频驱动转向代码驱动.主攻vs,c++然后先把界面东西做出来,然后在想后面的东西. 所以今天 [先上来看了 ...

  8. 关于IE调试模式下才能显示效果

    要去除console.log() 低版本IE 没有开启调试模式  console.log()会导致报错

  9. handlebar helper帮助方法

    handlebars相对来讲算一个轻量级.高性能的模板引擎,因其简单.直观.不污染HTML的特性.另一方面,handlebars作为一个logicless的模板,不支持特别复杂的表达式.语句,只内置了 ...

  10. 关于XML的DTD概述

    1 DTD概述 1.1 什么是DTD DTD(Document Type Definition),文档类型定义,用来约束XML文档.或者可以把DTD理解为创建XML文档的结构!例如可以用DTD要求XM ...