UVa 514 Rails(模拟栈)

题意  n辆火车按顺序依次进站  判断给定的出战顺序是否可能

用数组模拟模拟栈代表车站  车依次进站  每当栈顶火车序号与当前要出站的b[cur] 相等时 就让栈顶元素出栈  即top--

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 2000;
int b[N], c[N];
int main()
{
    int l, cur, top;
    while(scanf("%d", &l), l)
    {
        while(scanf("%d", &b[1]), b[1])
        {
            for(int i = 2; i <= l; ++i)
                scanf("%d", &b[i]);
            cur = 1, top = 0;
            for(int i = 1; i <= l; ++i)
            {
                if(i == b[cur]) cur++;
                else c[++top] = i;
                while(top && b[cur] == c[top])
                {
                    ++cur;
                    --top;
                }
            }
            printf("%s\n", top ? "No" : "Yes");
        }
        printf("\n");
    }
    return 0;
}

 Rails 

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 coaches
numbered in increasing order . 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 . 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 file 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 The
last line of the block contains just 0.

The last block consists of just one line containing 0.

Output

The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains Yes if
it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains No. In addition, there is one empty line after
the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last ``null‘‘ block of the input file.

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

时间: 2024-10-12 19:22:50

UVa 514 Rails(模拟栈)的相关文章

UVa 514 Rails(经典栈)

 Rails  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, i

UVA - 514 Rails 经典栈使用

题目大意:有一列n节车厢的火车要入栈,车厢从1到n,只能从小到大入栈. 现在给出一个出栈顺序,问能否实现 解题思路:如果栈顶元素大于要出栈的数,肯定就不能实现了. 如果栈顶元素小于要出栈的数,就继续入栈 如果栈定元素等于要出栈的数,就出栈 #include<cstdio> #include<algorithm> #include<stack> using namespace std; #define maxn 1010 int num[maxn], n; void so

UVa 514 Rails(栈的应用)

题目链接: https://cn.vjudge.net/problem/UVA-514 1 /* 2 问题 3 输入猜测出栈顺序,如果可能输出Yes,否则输出No 4 5 解题思路 6 貌似没有直接可以判定的方法,紫书上给出的方法是在进行一个入栈出栈操作,看是否能够构成原始的入栈序列. 7 */ 8 #include<cstdio> 9 #include<stack> 10 11 using namespace std; 12 int ok(int a[],int n); 13 i

UVa 514 - Rails

https://uva.onlinejudge.org/external/5/514.pdf 514 Rails 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 possib

[2016-02-03][UVA][514][Rails]

时间:2016-02-03 22:24:52 星期三 题目编号:UVA 514 题目大意:给定若干的火车(编号1-n),按1-n的顺序进入车站, 给出火车出站的顺序,问是否有可能存在 分析:    FIFO,用栈模拟一遍即可, 方法:    根据输入的顺序,从1-n开始,当前操作的为i 如果i是当前对应的编号,那么直接跳过(进入B) 如果不是,根据当前需求的编号,小于i,就从栈顶弹出一个元素, 看这个元素是否是需求的,是则继续.否则NO 1 2 3 4 5 6 7 8 9 10 11 12 13

Uva - 1513 Moive collection ( 模拟栈 + 树状数组基本操作 )

Uva - 1513 Moive collection ( 模拟栈 + 树状数组基本操作 ) 题意: 一个书架,原来所有的书都是按顺序摆好的,书的编号从1开始到n 操作: 取出一本书,统计在这本书之前有多少本书,统计完之后,将这本书放在书架的第一位. 如:  1 2 3 4 5取4   4 1 2 3 5 (取之前,有3本书在4前面,取完后,将4放在栈顶)取4   4 1 2 3 5 (取之前,有0本书在4前面,取完后,将4放在栈顶)取2   2 4 1 3 5 (取之前,有2本书在2前面,取完

poj 1363 Rails (栈的应用+STL)

Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24762   Accepted: 9715 Description There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds we

java 16 - 5 LinkedList模拟栈数据结构的集合

请用LinkedList模拟栈数据结构的集合,并测试 题目的意思是: 你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟. 1 package cn_LinkedList; 2 3 import java.util.LinkedList; 4 5 6 7 8 public class MyStack { 9 10 //定义一个LinkedList类的成员变量 11 private LinkedList list = null; 12 13 /** 14 * 构造方法 15

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr