Ytu 1011 Rails Stack

Source: Internet
Author: User

1011:rails time limit: $ Sec Memory Limit:
Submit: Solved: 6
[Submit] [Status] [Web Board] Description

There is a famous railway station in Poppush City. Country there is incredibly hilly. The station is built in 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 (see picture) and due to lack of available space it Could has only one track.

The local tradition is this every train arriving from the direction A continues in the direction B with coaches Reorgan Ized in some. Assume that the train arriving from the direction A have N <= the coaches numbered in increasing order 1, 2, ..., N. Th E chief for train reorganizations must know whether it's possible to marshal coaches continuing in the direction B so tha T their order would be A1, A2, ..., an. Help him and write a program that decides whether it was 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 that they can move themselves until they is on the track in the direction B. You can also suppose so at any time there can is located as many coaches as necessary in the station. But once a coach had entered it cannot return to the track in the direction A and also once it had left the St ation in the direction B it cannot return 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. 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's possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there are 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
51 2 3 4 55 4 1 2 3066 5 4 3 2 100
Sample Output
Yesnoyes
HINT

Source

Freeproblemset

AC Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int Main () {    int n;    int target[1010],i,stcak[1010];    while (Cin>>n&&n) {while        (Cin>>target[1]&&target[1]) {for            (i=2;i<=n;++i) cin>>target[i];            int top=0,a=1,b=1,ok=1;            while (b<=n) {                if (A==target[b]) {a++;b++;}                else if (Top&&stcak[top]==target[b]) {top--;b++;}                else if (a<=n) stcak[++top]=a++;                else {ok=0;break;}            }            if (ok==1) {cout<< "yes\n";}            else cout<< "no\n";        }        Putchar (' \ n ');    }    return 0;}

PS Output Pit Daddy

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ytu 1011 Rails Stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.