主题链接:

思路:

这道题就是一道简单的栈模拟。

。。

。我最開始认为难处理是当出栈后top指针变化了。

。当不满足条件时入栈的当前位置怎么办。这时候想到用一个Copy数组保持入栈记录就可以。

。当满足全部的火车都出栈时或者已经没有火车能够进栈了,那么久跳出。。

最后推断

是否出栈的火车是否达到n。。。

题目:

Rails
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24940   Accepted: 9771

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


代码为:

#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;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

poj1363Rails(栈模拟)的更多相关文章

  1. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  2. UVALive 3486/zoj 2615 Cells(栈模拟dfs)

    这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...

  3. UVALive 7454 Parentheses (栈+模拟)

    Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...

  4. 【LintCode·容易】用栈模拟汉诺塔问题

    用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...

  5. 51Nod 1289 大鱼吃小鱼 栈模拟 思路

    1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...

  6. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

  7. HDOJ 4699 Editor 栈 模拟

    用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  8. 吐泡泡(2018年全国多校算法寒假训练营练习比赛(第二场)+栈模拟)+Plug-in(codeforces81A+栈模拟)

    吐泡泡题目链接:https://www.nowcoder.com/acm/contest/74/A 题目: 思路: 这种题目当初卡了我很久,今天早训时遇到一个一样得题,一眼就想到用栈模拟,就又回来把这 ...

  9. 【栈模拟dfs】Cells UVALive - 3486

    题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...

随机推荐

  1. 解决PowerDesigner中DBMS设置的问题(Repost)

    创建物理模型时DBMS下拉框是空的,没值,以前从来没遇到过这种现象,开始以为PowerDesigner安装软件的问题,不过装了又卸,卸了又装,结果还是那样,现在找到答案了:点击DBMS后面的黄色文件图 ...

  2. 给进程分配cpu核心

    新负责的程序采用生产者和消费者的模式,生产者的速度非常快,数据几乎都在内存里,处理起来很快.而消费者要频繁的I/O.所以打算给生产者和消费者分配不一样的核心. 生产者只需要一个核心就够了,其余分配给消 ...

  3. Hash算法原理理解

    我们有很多的小猪,每个的体重都不一样,假设体重分布比较平均(我们考虑到公斤级别),我们按照体重来分,划分成100个小猪圈. 然后把每个小猪,按照体重赶进各自的猪圈里,记录档案. 好了,如果我们要找某个 ...

  4. 开发框架CIIP

    github开源:企业级应用快速开发框架CIIP WEB+WIN+移动端   简介 CIIP是基于XAF开发的开源信息系统框架.CIIP最常见的应用场景是基于数据库的企业级应用程序,例如供应链系统,E ...

  5. 在非gui线程使用QMessageBox

    最近我写项目的时候遇到一个奇怪的需求,要在工作线程内,根据某个情况弹出一个MessageBox 但是Qt提供的MessageBox只可以在gui线程(主线程)使用,于是我就对QMessageBox封装 ...

  6. java.util.Timer分析源码了解原理

    Timer中最主要由三个部分组成: 任务 TimerTask .  任务队列: TaskQueue queue 和 任务调试者:TimerThread thread 他们之间的关系可以通过下面图示: ...

  7. Linux下的经常使用性能查询命令top、vmstat、gprof、pidstat之对照

    (1)查看各个CPU核的使用情况 sudo top -d 1 进入之后,按1,会出现以下的CPU使用情况,当中us列反映了各个CPU核的使用情况,百分比大说明该核在进行紧张的任务. (2)查看哪个进程 ...

  8. 按模板打印word防止并发操作

    /// <summary> /// /// <summary> /// 打印人员备案表 /// </summary> /// <param name=&quo ...

  9. CSS定位深入理解 完全掌握CSS定位 相对定位和绝对定位

    其实前面的标准流和浮动流都很理解,就是定位不太好理解,特别是相对定位和绝对定位,很多刚开始学的同学不好区分.因此这里,小强老师和大家一起分享CSS定位的学习. 通过我们前面的学习,我们网页布局方法: ...

  10. Oracle存储过程返回一张表数据

    在oracle数据库中你要在程序里得到一张表的数据就必须先创建游标和SQL Service不一样. --创建游标create or replace package pkg_Dataas type re ...