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个节点各有的子节点个数 ...
 
随机推荐
- 项目中引用ThinkPHP框架
			
ThinkPHP是一个宽度.兼容且简单的国产的轻量级框架,具有优良的性能,并且非常注重易用性. 那么,我们该如何将ThinkPHP引入自己的项目中,使得自己的项目可以使用这款优良的框架呢? 首先介绍下 ...
 - (10)Xamarin.Android - 储存数据于Windows Azure
			
原文 Xamarin.Android - 储存数据于Windows Azure 如何将Xamarin.Android 与Windows Azure做结合,将Android APP上的数据丢到云端去储存 ...
 - MFC上下浮动与渐入渐出消息提示框实现
			
类似QQ与360软件,消息提示有两种.上下浮动.渐入渐出. 1.上下浮动提示框实现 机制,定时器响应上下浮动消息. 主要API:MoveWindow. 源码如下UpDownTipDlg.h.UpDow ...
 - poj2000---和1969一样的模板
			
#include <stdio.h> #include <stdlib.h> int main() { int d; while(scanf("%d",&a ...
 - [Oracle] Listener的动态注册
			
在有Oracle Listener的动态注册之前,采用的是静态注册,所谓静态注册是指Oracle实例在启动时,读取listener.ora里的配置,然后注册到Listener,它主要有两个缺点: 1. ...
 - javascript if 与 if else 的一点小认识
			
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
 - Android_按钮被按下效果的实现(selector选择器)
			
在很多刚入门的新手在开发实例的过程中,经常会遇到要按下某个ImageView时,需要加入确认感的时候.需要在按下的时候,控制ImageVIew内图片的显示. 在我是新手的时候,也这样做过.所以这里简单 ...
 - c++ 回调类成员函数实现
			
实现类成员函数的回调,并非静态函数:区分之 #ifndef __CALLBACK_PROXY_H_ #define __CALLBACK_PROXY_H_ template <typename ...
 - iWeb峰会见闻
			
8.16去参加了iWeb峰会,一大早8点过10分就到了,发现外面已经排起了长队(说明影响力越来越大,关注的人越来越多了.) 此次大会参与的企业也越来越多,当然是有目的而来~ 上午 google商业合作 ...
 - 关于ajax的那些事
			
什么是ajax AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. Ajax包含下列技术:基于 ...