toj-1036-火车进站

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

方法一:采用栈操作

#include <iostream>
#include <stack>
using namespace std;
int main()
{
    stack <int>instation;
    int num;
    int outstation[10000];
    while(cin >> num && num!=0)
    {
        while(cin >> outstation[0] && outstation[0])
        {
            for(int i = 1;i < num;i++)
                cin >> outstation[i];
            int i;
   int indexout = 0;
            for( i=1; i <= num;)
            {
                instation.push(i++);
                while(outstation[indexout] != instation.top() && i <= num + 1)
                {
                    instation.push(i++);
                }
                while(!instation.empty() && instation.top() == outstation[indexout])
                {
                    instation.pop();
                    indexout++;
                }
            }
            if(i==indexout+1)
                cout << "Yes" << endl;
            else
                cout << "No" <<endl;
            while(!instation.empty())
                instation.pop();
        }
        cout << endl;
    }
    return 0;
}

方法二:用数组模拟栈

#include <iostream>
using namespace std;

int main()
{
    int num, to[100000], mid[100000];

    while(cin >> num && num)
    {
        while(cin >> to[0] && to[0])
        {
            for(int i=1; i<num; i++)
            {
                cin >> to[i];
            }
            int top = 0, j=1, i;
            for(i=0; i<num && j<=num+1;)
            {
                if(j == to[i])  j++, i++;
                else if(top && mid[top-1]==to[i])
                    top--, i++;
                else mid[top++] = j++;
            }
            if(i<num)  cout << "No" << endl;
            else       cout << "Yes" << endl;
        }
        cout << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一列货运列车共有 n 节车厢,每节车厢将停放在不同的车站。假定 n 个车站的编号分别 为 1~n,车厢的编号与它们的目的地相同。货运列车按照从第 n 站至第 1 站的次序经过这 些车站。为了便于从列车上卸掉相应的车厢,必须重新排列车厢,使各车厢从前至后按编号 1~n 的次序排列。当所有的车厢按照这种次序排列时,在每个车站只卸掉最后一节车厢 即可。可以在一个转轨站里完成车厢的重排工作,在转轨站中有一个入轨,一个出轨和k 个缓冲铁轨(位于入轨和出轨之间)。 图 3-1 给出了一个转轨站, 其中有 k=3 个缓冲铁轨 H1,H2 和 H3。开始时,n节车厢的货车从入轨处进入转轨站,车厢重排结束时各车厢按照编号1至编号n的次序从出轨处离开转轨站。在图 3-1(a)中,n=9,车厢从后至前的初始次序为 5,8,1,7,4,2,9,6,3。图 3-1(b)给出按所要求的次序重新排列后的结果。 为了重排车厢,从前至后依次检查入轨上的所有车厢。如果正在检查的车厢就是下一个满足排列的要求的车厢,可以直接把它放到出轨上。如果不是,则把它移动到缓冲铁轨上, 直到按输出次序要求轮到它时才将它放到出轨上。由于缓冲铁轨上车厢的进和出都是在其顶 部进行的,因此缓冲铁轨是按照 LIFO 的方式使用的。在重排车厢过程中,仅允许以下移动:  车厢可以从入轨移动到一个缓冲铁轨的顶部或者出轨上;  车厢可以从一个缓冲铁轨的顶部移动到的出轨上;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值