<OJ_Sicily>Rails

本文介绍了一种基于栈的数据结构解决列车调度问题的方法。通过模拟列车进出站的过程,验证给定的车厢顺序是否可以通过特定的调度方式实现。文章提供了一个C++程序示例,展示了如何用栈来实现这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

题目解释:这道题实际上求的是对于1~N个数按顺序入栈,是否能够实现给定的出栈顺序。要是能够实现就输出“Yes”要是不能够实现就输出“No”。这道题其实主要是使用一个栈和一个while循环就能够解决。这道题对输入输出有要求。

解题思路:这道题要求先输入一个数N,表示将有N个数实现入栈和出栈操作,对于接下来有不确定个case,以0结束,比如说输入为5,表示有5个数进行操作,但我们无法知道接下来有多少组case需要进行测试,每一组都是5个数的排序。并且题目要求不同的N之间要输出一个空行。对于这种要求,我们首先设置一个数组outOrder,输入第一个数N后,进入循环,如果N=0直接跳出。对于N不为0,接下来进入第二个while循环,在这个while循环里面,先从输入端读取一个数在outOrder[0],因为我们无法预测下一个是包含N个数的一组case还是一个“0”表示结束,因此我们将输入第一个数,然后进行判断outOder[0]是否为0,要是为0直接跳出,说明当前block已经结束,输出一个空行,否则进行剩下N-1个数的输入。

对于存在outOrder数组中的N个数,设置当前索引为0,表示指向带出栈的数。对栈压入第一个数后,开始进行while循环,直到所有的数都push进栈中,然而在这个过程中,只要遇到当前栈顶的数等于带出栈的数,便将该数pop出来,否则继续进行push操作。

#include <iostream>
#include <stack>
using namespace std;
int outOrder[1005];
int main(int argc, const char * argv[]) {
    // insert code here...
    int trainNum;
    while (cin >> trainNum) {
        if (trainNum == 0) break;
        while (1) {
            cin >> outOrder[0] ;
            if (outOrder[0] == 0) break;
            for (int i = 1; i < trainNum; i++) {
                cin >> outOrder[i];
            }
            stack<int> st;
            int preIndex = 0, inTrain = 1;
            while (inTrain <= trainNum) {
                st.push(inTrain);
                while (!st.empty() && st.top() == outOrder[preIndex]) {   // 如果当前栈顶的数跟当前需要输出的数一致,则进行出栈操作
                    st.pop();
                    preIndex ++;
                }
                inTrain ++;
            }
            if (st.empty()) cout << "Yes" << endl;
            else cout << "No" << endl;
        }
        cout << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值