UVa 514: Rails

怎么进来就想想它怎么回去。

#include <iostream>
#include <stack>

using namespace std;

int main()
{
    stack<int> in_station;
    int num_carriage;

    while(cin >> num_carriage && num_carriage != 0)
    {
        if(num_carriage < 1 || num_carriage > 1000)
            continue;

        int *seq_carriage = new int[num_carriage];
        while(cin >> seq_carriage[0] && seq_carriage[0] != 0)
        {
            // in_station.push(seq_carriage[0]);
            int seq_top = num_carriage + 1;

            for(int i = 1; i < num_carriage; i++)
            {
                cin >> seq_carriage[i];
            }

            int result = 0;
            for(int i = num_carriage - 1; i >= 0; i--)
            {
                int top_stack;
                result = 0;

                // push to the temporary station
                if(!in_station.empty())
                {
                    top_stack = in_station.top();
                    result = 1;
                }   

                if (result && top_stack > seq_carriage[i])
                {
                    break;
                }

                in_station.push(seq_carriage[i]);
                top_stack = in_station.top();

                // pop the train.
                while(seq_top == top_stack + 1 )
                {
                    in_station.pop();
                    seq_top = top_stack;

                    if (in_station.empty())
                    {
                        break;
                    }
                    top_stack = in_station.top();
                }   
            }

            if(!in_station.empty())
                cout << "No" << endl;
            else 
                cout << "Yes" << endl;  

            while(!in_station.empty())
            {
                in_station.pop();
            }       
        }
        cout << endl;

        delete []seq_carriage;
    }   
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值