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

有n辆火车  按1到n的顺序进站  最后进站的车可以在任何时候出去  判断给定的出站序列是否可能

火车只有两种状态  从A进站  或者从站到B   模拟栈的操作就行了

令A表示A中当前待进站的第一辆火车  tar[B]表示出站序列中当前应该出站的火车   sta为火车站

当A==tar[B]的时候  A进站马上出战   否则当站中最后一辆==tar[B]时   这辆车出站  都不满足就只能A中的最前面的火车进站

当n辆火车全部进站  而站中还有火车是   给定的出战序列就是不可能的

#include<cstdio>
#include<stack>
using namespace std;
const int N = 1005;
int n, tar[N], A, B;
int main()
{
    while (scanf ("%d", &n), n)
    {
        while (scanf ("%d", &tar[1]), tar[1])
        {
            for (int i = 2; i <= n; ++i)
                scanf ("%d", &tar[i]);
            stack<int> sta;
            A = B = 1;
            bool ok = true;
            while (B <= n)
            {
                if (A == tar[B])
                {   ++A; ++B;   }
                else if (!sta.empty() && sta.top() == tar[B])
                {   sta.pop();  ++B;  }
                else if (A <= n)
                    sta.push (A++);
                else
                {   ok = false;  break;  }
            }
            printf (ok ? "Yes\n" : "No\n");
        }
        printf("\n");
    }
    return 0;
}

UVa 514 Rails(经典栈)

时间: 2024-11-05 06:10:36

UVa 514 Rails(经典栈)的相关文章

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

题意  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"

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

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

例题6-2(Rails, UVa 514)

题目链接 给定入栈顺序,问能否以给出的顺序出栈. 众所周知,栈的特点是先入后出. 此特点在该题中体现为对于当前需要出栈的元素(要想先出),必须位于栈顶或者还未入栈(必须后入). 用数组target来存储出栈序列,target[B_now] 表示当前需要驶入B的车厢(即当前需要出栈的元素),A_first表示当前A中第一个车厢(即下一个入栈元素) 1 #include <iostream> 2 #include <stack> 3 using namespace std; 4 5 c

Rails,uva 514

题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例如,出栈顺序(5 4 1 2 3)是不可能的,但是(5 4 3 2 1)是可能的. 题目分析: 为了重组车厢,借助中转站,对于每个车厢,一旦从A移入C就不能回到A了,一旦从C移入B,就不能回到C了,意思就是A->C和C->B.而且在中转站C中,车厢符合后进先出的原则.故这里可以看做为一个栈. 题目

UVa 514 数据结构栈

背景:1A 思路:栈模拟 我的代码: #include <set> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <map> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm&g