传送门: POJ1363
关键词: 栈;
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.
PopPush城有一个著名的、建于上个世纪的铁路。PopPush城的乡村山多到难以置信。不幸的是,资金在那个时候非常有限。仅仅可以建路面铁轨。此外,事实证明该站可能只是一个死胡同(见图),由于缺乏可用空间,它可能只有一个铁轨。
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.
当地传统的火车方向是从A方向到达的每一列火车都继续朝B方向行驶,车厢可以以某种方式重组。假设从A方向到达的火车具有N<=1000个车厢,这些车厢以递增的顺序1,2,…,N编号。负责车厢调度的工作人员需要知道能否使它以a1,a2,…,aN的顺序从B方向驶出。请你帮他写一个程序,用来判断能否得到指定的车厢顺序。你可以假定在进入车站之前每节车厢之间都是不连着的,并且它们可以自行移动,直到处在B方向的铁轨上。另外假定车站里可以停放任意多节的车厢。但是一旦车厢进入车站,它就不能再回到A方向的铁轨上了,并且一旦它进入B方向的铁轨后,它就不能再回到车站。
Input
The input consists of blocks of lines. Each block except the last describes o