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个节点各有的子节点个数 ...
随机推荐
- vb listview 的常用操作
常用操作:获取当前行数和列数: MsgBox "行数:" & ListView1.ListItems.Count & "列数:" & L ...
- 百度地图Label 样式:label.setStyle
创建文本标注对象设置样式的时候,其中的backgroundColor属性居然还支持透明啊,不过改变数值好像对效果没有影响 var numLabel = new BMap.Label(num); num ...
- odi增量更新策略
增量更新策略:通过一个“update key”比较流数据记录与目标表中的记录比较进行数据整合.具有相同“update key”的记录当相关联列不同时将被更新:在目标表中不存在的记录将被插入.这种方式用 ...
- label 标签
<label> 标签为 input 元素定义标注内容 label 元素不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.如果您在 label 元素内点击文本,就会触发此控件.就是 ...
- 学习使用crosswalk
1.创建一个工程,最好使用Swift语言,或者也可以叫做雨燕语言,名字很好听吧,功能也是很强大的.言归正传,创建好工程后,需要用CocoaPod工具,不懂的童鞋自行google吧. 根据CrossWa ...
- City Game(动态规划)
City Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- SSH整合方案2
[案例3]SSH整合_方案2 ** 案例描述 两个知识点的演示 其一,SSH整合的第二个方案 其二,Spring+JDBC+Struts2 参考代码 31) 使用工程spring4 32 ...
- 切点算法模板(Cut-vertex)
下面是一个模板被切割点,也cut_vertex_num[]排列(array)什么是切 - 点记录 Int cut_vertex_num[]; void dfs(int cur,int pa) { in ...
- 解密:LL与LR解析 2(译,完结)
由于GFW,我无法联系到作者,所以没有授权,瞎翻译的.原文在这里[http://blog.reverberate.org/2013/07/ll-and-lr-parsing-demystified.h ...
- 谷歌识图、google识图如何知道图片相似?
转自:http://www.hahait.com/news/185.html 原理非常简单易懂.我们可以用一个快速算法,就达到基本的效果.这里的关键技术叫做“感知哈希算法”(Perceptual ha ...