POJ1363 Rails "Stack" "Stacks"

Source: Internet
Author: User

RailsTime limit:1000ms Memory Limit:10000ktotal submissions:25638Accepted:10068description

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 reorganize D 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 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. 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

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


Source

Central Europe 1997


As shown, it is known that the train will be inbound from a and then outbound from C. The train is at the pit

Order for 1~n, and now give you the order of outbound. Q: Can I change the train outbound order through the platform?

To implement outbound in the order given.

Idea: To see the platform as a stack, in the order of 1~n to traverse the original sequence of the train, first into the stack, such as

The train number at the top of the stack will be the same as the number of the outbound order. Then the train is out of the stack,

Until all the trains in the stack that meet the outbound order are out of the station, otherwise they will be in the stack. Finally judge the

Whether the train is out of the station. If all outbound, output Yes, otherwise output No.


#include <cstdio> #include <string.h> #include <stack>using namespace std;const int maxn = 1010;stack <int> s;int Train[maxn];int Main () {    int N;    while (~SCANF ("%d", &n) && N)    {while        (scanf ("%d", &train[1]) && train[1])        {            for (int i = 2; I <= N; i++)                scanf ("%d", &train[i]);            int B = 1;            for (int i = 1; I <= N; i++)            {                s.push (i);                while (!s.empty () && s.top () = = Train[b])                {                    s.pop ();                    b++;                }            }            if (B = = n+1)                printf ("yes\n");            else                printf ("no\n");            memset (Train,0,sizeof (train));        }        printf ("\ n");    }    return 0;}




POJ1363 Rails "Stack" "Stacks"

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.