poj1363Rails(栈模拟)
主题链接:
思路:
这道题就是一道简单的栈模拟。
。。
。我最開始认为难处理是当出栈后top指针变化了。
。当不满足条件时入栈的当前位置怎么办。这时候想到用一个Copy数组保持入栈记录就可以。
。当满足全部的火车都出栈时或者已经没有火车能够进栈了,那么久跳出。。
最后推断
是否出栈的火车是否达到n。。。
题目:
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 24940 | Accepted: 9771 |
Description
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
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
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
代码为:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=1000+10;
int in[maxn],out[maxn],temp[maxn];
int main()
{
int n,tail,top,cal,pd,u,flag;
ind:while(~scanf("%d",&n))
{
flag=1;
if(n==0) return 0;
while(1)
{
cal=0;
pd=top=tail=1;
for(int i=1;i<=n;i++)
{
in[i]=i;
scanf("%d",&out[i]);
if(out[1]==0)
{
flag=0;
break;
}
}
if(flag==0)
break;
temp[0]=0;
temp[pd]=in[pd];
while(cal<=n&&pd<=n)
{
if(temp[top]==out[tail])
{
top--;
tail++;
cal++;
}
else
temp[++top]=in[++pd];
if(cal>=n||pd>n)
break;
}
if(cal>=n)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
printf("\n");
}
return 0;
}
#include<cstring>
#include<iostream>
using namespace std; const int maxn=1000+10;
int in[maxn],out[maxn],temp[maxn]; int main()
{
int n,tail,top,cal,pd,u,flag;
ind:while(~scanf("%d",&n))
{
flag=1;
if(n==0) return 0;
while(1)
{
cal=0;
pd=top=tail=1;
for(int i=1;i<=n;i++)
{
in[i]=i;
scanf("%d",&out[i]);
if(out[1]==0)
{
flag=0;
break;
}
}
if(flag==0)
break;
temp[0]=0;
temp[pd]=in[pd];
while(cal<=n&&pd<=n)
{
if(temp[top]==out[tail])
{
top--;
tail++;
cal++;
}
else
temp[++top]=in[++pd];
if(cal>=n||pd>n)
break;
}
if(cal>=n)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
printf("\n");
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
poj1363Rails(栈模拟)的更多相关文章
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- UVALive 7454 Parentheses (栈+模拟)
Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...
- 【LintCode·容易】用栈模拟汉诺塔问题
用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...
- 51Nod 1289 大鱼吃小鱼 栈模拟 思路
1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...
- Code POJ - 1780(栈模拟dfs)
题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...
- HDOJ 4699 Editor 栈 模拟
用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 吐泡泡(2018年全国多校算法寒假训练营练习比赛(第二场)+栈模拟)+Plug-in(codeforces81A+栈模拟)
吐泡泡题目链接:https://www.nowcoder.com/acm/contest/74/A 题目: 思路: 这种题目当初卡了我很久,今天早训时遇到一个一样得题,一眼就想到用栈模拟,就又回来把这 ...
- 【栈模拟dfs】Cells UVALive - 3486
题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...
随机推荐
- PHP判断图片是否存在和jquery中load事件对图片的处理
在公司的图片服务器中,同一个产品一般会存在对应的大图和缩略图.因此,我们在开发手机端的web网站时,默认使用的是产品图片的缩略图,查询数据库时获取的是缩略图的路径.但是,不知什么原因,时不时的,测试的 ...
- codeforces 8D Two Friends 二分+ 判断三个圆是否有公共交点
题目链接 有两个人x, y, 现在在A点, x要直接去B点, y要先去C点在去B点, 现在给出x, y两人可以行走的最大距离T1, T2, 求出他们从A点出发之后, 可以走的最长的公共路径. 我们先看 ...
- Protection 5 ---- Priviliege Level Checking 2
CPU不仅仅在程序访问数据段和堆栈段的时候进行权限级别检查,当程序控制权转换的时候也会进行权限级别检查.程序控制权转换的情况很多,各种情况下检查的方式以及涉及到的检查项都是不同的.这篇文章主要描述了各 ...
- 强烈推荐visual c++ 2012入门经典适合初学者入门
强烈推荐visual c++ 2012入门经典适合初学者入门 此书循序渐进,用其独特.易于理解的教程风格来介绍各个主题,无论是编程新手,还是经验丰富的编程人员,都很容易理解. 此书的目录基本覆盖了Wi ...
- windows的命令行工具和DOS工具的区别
很多的系统管理员可能认为命令行是程序员编程用的,这是不对的,其实命令行是另一种用来管理计算机的接口.1 命令行窗口 Windows NT/Windows 2000以后的操作系统为用户提供 ...
- 【转】stdin, stdout, stderr 以及重定向
详细见: http://my.oschina.net/qihh/blog/55308 stdin是标准输入文件,stdout是标准输出文件,stderr标准出错文件. 程序按如下方式使用这些文件: 标 ...
- C#中经常使用的几种读取XML文件的方法
XML文件是一种经常使用的文件格式,比如WinForm里面的app.config以及Web程序中的web.config文件,还有很多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖 ...
- Codechef Nuclear Reactors 题解
There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded onto chamber 0. ...
- Android 常用开发类库
android.app :提供高层的程序模型.提供基本的运行环境 android.content :包含各种的对设备上的数据进行访问和发布的类 android.database :通过内容 ...
- 2013年 ACM 有为杯 Problem I (DAG)
有为杯 Problem I DAG 有向无环图 A direct acylic graph(DAG),is a directed graph with no directed cycles . T ...