poj1363Rails(栈模拟)

题目链接:

啊哈哈,点我点我

思路:

这道题就是一道简单的栈模拟。。。。我最开始觉得难处理是当出栈后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

Central Europe 1997

代码为:

#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(栈模拟)

时间: 2024-10-10 00:47:31

poj1363Rails(栈模拟)的相关文章

hdu1515 dfs栈模拟

Anagrams by Stack Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1513    Accepted Submission(s): 690 Problem Description How can anagrams result from sequences of stack operations? There are tw

用两个栈模拟实现一个队列

题目:如何用两个栈模拟实现一个队列?  如果这两个堆栈的容量分别是m和n(m>n),你的方法能保证的队列容量是多少?(这里讨论的是顺序栈,如果是链式栈的话完全没有必要考虑空间) 分析:栈的特点是“后进先出(LIFO)”,而队列的特点是“先进先出(FIFO)”.用两个栈模拟实现一个队列的基本思路是:用一个栈作为存储空间,另一个栈作为输出缓冲区,把元素按顺序压入两栈(模拟的队列),并按此顺序出队并输出即可. 如下图,用容量为m的栈作为存储空间,容量为n的栈作为输出缓冲区,一开始先将n个元素压入(pu

ACM学习历程——UVA 127 &quot;Accordian&quot; Patience(栈;模拟)

Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patience, the rules for which are as follows: Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate n

HDOJ 4699 Editor 栈 模拟

用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1913    Accepted Submission(s): 591 Problem Description Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2 Sample Output 2 3 Hint The fol

web前端面试系列 - 数据结构(两个栈模拟一个队列)

一. 用两个栈模拟一个队列 思路一: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时将数据压人s1 3. 出队时将s1弹出,并压人s2,然后弹出s2中的顶部数据,最后再将剩余数据弹出s2,并压人s1. 思路二: 1. 一个栈s1作为数据存储,另一个栈s2,作为临时数据存储. 2. 入队时,判断s1, 是否为空,如果不为空,则将数据直接压入s1, 如果为空,则将s2中的数据全部倒入s1,在将数据压人s1. 3. 出队时,判断s2, 是否为空,如果不为空,则直接弹出s2

自定义栈的实现及使用两个栈模拟队列

一,使用单链表实现栈 ①栈需要一个栈顶指针 ②栈的基本操作有出栈和入栈,以及判断栈是否为空 ③单链表中每个结点表示一个栈元素,每个结点有指向下一个结点的指针.因此,在栈内部需要实现一个单链表.代码如下: public class Stack<T extends Comparable<? super T>>{ private class Node{ T ele; Node next; public Node(T ele) { this.ele = ele; } } Node top;

【干货】容器适配器实现两个栈模拟队列

用两个栈模拟队列的思想就是"倒水思想",这里我们用自定义类型模拟出线性表,再用线性表做容器实现栈的数据结构,最后用栈来实现队列,代码如下: #include<iostream> #include<string> #include<cassert> struct __TrueType//类型萃取 { bool Get() { return true; } }; struct __FalseType { bool Get() { return false

深度优先搜索入门:POJ1164城堡问题(递归、用栈模拟递归)

将问题的各状态之间的转移关系描述为一个图,则深度优先搜索遍历整个图的框架为:Dfs(v) {if( v 访问过)return;将v标记为访问过;对和v相邻的每个点u: Dfs(u);}int main() {while(在图中能找到未访问过的点 k) Dfs(k);} 例题: POJ1164 The Castle Description 1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#-

字符串反转,栈模拟(ZOJ1151)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151 这里可以用栈模拟,也可以用STL,reverse();函数. 但是我这里用栈模拟,PE了,了解一下这个做法吧. #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <string> usin