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个节点各有的子节点个数 ...
随机推荐
- keyDown keyPress keyUp 事件的区别
keyDown keyPress keyUp 事件的区别 一 触发顺序 显而易见,事件发生的顺序是: keydown --> keypress --> keyup 当按住一个键一段时间后 ...
- Java安全学习
http://blog.csdn.net/wbw1985/article/details/5506515 http://blog.csdn.net/wbw1985/article/details/60 ...
- hdu4288 Coder
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- elk之nginx
elk之nginx: ignore_older => 86400,不处理一天以前的文件. zjtest7-frontend:/usr/local/logstash-2.3.4/config# c ...
- Google Maps API V2
1. 在AndroidManifest.xml的application节点中,添加Google play service的版本号: <meta-data android:name="c ...
- [LeetCode][Python]Container With Most Water
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...
- c++基础 之 面向对象特征一 : 继承
class Base { public: void f() { cout<<"void f()"<<endl<<endl; } void f(i ...
- 如何在MFC中操作资源句柄
如何获取动态库中对话框相关资源,避免因资源问题报错? AfxGetResourceHandle用于获取当前资源模块句柄AfxSetResourceHandle则用于设置程序目前要使用的资源模块句柄. ...
- web - 清除浮动
最理想的方式为 伪类 + content : 例如 div:after{content:"";display:block;clear:both;} div{zoom:1;} 另外, ...
- Nutch安装的几个网址
RunNutchInEclipse - Nutch Wiki http://wiki.apache.org/nutch/RunNutchInEclipse Index of /apache/nut ...