Ultraviolet A 514 rails (simulated stack)

Source: Internet
Author: User

N trains arrive in order to determine whether the given playing order is possible

Simulate the stack with arrays to represent the arrival of station vehicles in turn. Whenever the number of the train on the stack is equal to the number of B [cur] on the current station, the elements on the top of the stack are pushed out of the stack, that is, top --

#include<cstdio>#include<cstring>using namespace std;const int N = 2000;int b[N], c[N];int main(){    int l, cur, top;    while(scanf("%d", &l), l)    {        while(scanf("%d", &b[1]), b[1])        {            for(int i = 2; i <= l; ++i)                scanf("%d", &b[i]);            cur = 1, top = 0;            for(int i = 1; i <= l; ++i)            {                if(i == b[cur]) cur++;                else c[++top] = i;                while(top && b[cur] == c[top])                {                    ++cur;                    --top;                }            }            printf("%s\n", top ? "No" : "Yes");        }        printf("\n");    }    return 0;}

Rails

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 cocould be only a dead-end one (see picture) and due to lack of available space it cowould 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 coaches numbered in increasing order. 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. 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 your 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 file consists of blocks of lines. each block should t 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 the last line of the block contains just 0.

The last block 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 contains YesIf it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input file. there is no line in the output file corresponding to the last ''null'' block of the input file.

Sample Input

51 2 3 4 55 4 1 2 3066 5 4 3 2 100

Sample output

YesNoYes



Ultraviolet A 514 rails (simulated 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.