POJ 1363 Rails

时间:2022-09-06 11:31:27
Rails
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21728   Accepted: 8703

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. 
POJ 1363 Rails
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
题目大意:一串数字从1到n入栈,输入一串数字num,问这串数字的出战顺序能否为num。
#include <stdio.h>

int main()
{
int n, num[], Stack[], top = -, nCount = -, index, l;
while(scanf("%d", &n )&& n != )
{
l = ;
index = ;
nCount = -;
top = -;
while(scanf("%d", &num[++nCount]) && num[] != )
{
index++;
if (index == num[nCount])
{
l++;
}
else
{
Stack[++top] = index;
}
for (int i = ; i < n; i++)
{
scanf("%d", &num[++nCount]);
Stack[++top] = ++index;
while(num[l] == Stack[top] && top >= )
{
top--;
l++;
}
}
if (top == -)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
index = ;
nCount = -;
top = -;
l = ;
}
printf("\n");
}
return ;
}
														
		

POJ 1363 Rails的更多相关文章

  1. POJ 1363 Rails(栈)

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

  2. OpenJudg &sol; Poj 1363 Rails

    1.链接: http://poj.org/problem?id=1363 http://bailian.openjudge.cn/practice/1363 2.题目: Rails Time Limi ...

  3. poj 1363 Rails 解题报告

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

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

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

  5. poj 1363 Rails &lpar;【栈的应用】 刘汝佳的写法 &ast;学习&rpar;

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

  6. POJ 1363 Rails&lpar;栈&rpar;

    思路:将出车站的顺序存入数组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. css属性在各种浏览器上的兼容性

    transform:在360急速浏览器不支持,在IE兼容性上看IE浏览器的版本,IE9+是支持.

  2. Programming in Lua读书笔记

         Lua的长处之一就是可以通过新类型和函数来扩展其功能.动态类型检查最大限度允许多态出现,并自动简化调用内存管理的接口,因为这样不需要关心谁来分配内存谁来释放内存,也不必担心数据溢出.高级函数 ...

  3. 从出租车司机到大BOSS的转型之路

    来深圳之前,曾有人这样告诉我:在深圳千万不能以貌取人,打扮不起眼,也许他转身开的座驾就是宝马.奔驰;不管一个人多么邋遢俗气,也别瞧不起人家,也许他的手提袋里就是成捆的人民币现金;不管一个人打扮的多么土 ...

  4. M1分数分配

    进过第一轮迭代我们依据工作量及质量决定分配分数方案: 王皓南 24.5分 黄宇冰 24分 申开亮 23.5分 许晋 21分 王宇杰 17分 吴润凡 16分 巴丹益昔 14分

  5. wordpress无法安装这个包。&colon; PCLZIP&lowbar;ERR&lowbar;MISSING&lowbar;FILE &lpar;-4&rpar; &colon; Missing archive file &&num;39&semi;C&colon;&bsol;WINDOWS&bsol;TEMP&sol;wordpress-4&period;tmp&&num;39&semi;

    朋友的wp博客好久没管理了,让ytkah帮忙打理一下,进到后台发现版本还是3.9的,那是比较早以前的版本了,早该升级了. 在升级wordpress时出现以下错误: 无法安装这个包: PCLZIP_ER ...

  6. BZOJ 1072 &lbrack;SCOI2007&rsqb;排列perm

    1072: [SCOI2007]排列perm Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1268  Solved: 782[Submit][Sta ...

  7. php-fpm死机解决办法,脚本后台自动重启

    本人用nginx+php7搭建了一台服务器,因为请求量太大,而且php里面又有挂起的任务,导致php-fpm在高峰期的时候经常死掉,吧php-fpm的最大进程数已经改到1000了,还是吃不消,cpu也 ...

  8. Webstorm 激活破解

    2017-06-15更新 之前都是使用2017.2.27的方法,版本是2017.1.1,还没提示过期,但是根据评论说这个链接已经失效了,评论也给出了个新地址:http://idea.iteblog.c ...

  9. sqoop1&period;4&period;6从mysql导入hdfs&bsol;hive&bsol;hbase实例

    //验证sqoop是否连接到mysql数据库sqoop list-tables --connect 'jdbc:mysql://n1/guizhou_test?useUnicode=true& ...

  10. Berkeley Packet Filter &lpar;BPF&rpar; BCC

    http://www.brendangregg.com/ebpf.html https://qmonnet.github.io/whirl-offload/2016/09/01/dive-into-b ...