514-rails
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=455
There is a famous railway station into Poppush City. Country there is incredibly hilly. The station was built at last century. Unfortunately, funds were extremely limited that time. It is possible to establish only a surface track. Moreover, it turned out that the station could is only a dead-end one ("picture") and due to lack of available Could have only one track.
The local tradition is this every train arriving from the direction A continues in the direction B with coaches D in some way. Assume that the train arriving to the direction A has coaches numbered in increasing order. The chief for train reorganizations must know whether it are possible to marshal coaches in the continuing B so T Hat their order would be. Help him and write a program that decides whether it's possible to get the required order of coaches. You can assume this single coaches can is disconnected from the train before they enter the station and this they can move themselves until they are on the track in the direction B. You can also suppose this at any time there can is 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 St ation in the direction B it cannot return to the station.
Input
The input file consists of blocks of lines. Each blocks except the last describes one train and possibly more requirements for its reorganization. In the ' the ' block there is the Integern described above. In the ' next lines ' of the ' block there ' is a permutation of the ' the ' of the ' block contains just 0.
The last blocks 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 Containsyes if it's possible to marshal the coaches in the order required on the corresponding Line of the input file. otherwise it containsno. In addition, there are one empty line over the lines corresponding to one block of the input file. There is no. 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