poj1363 Rails (Stack emulation)

Source: Internet
Author: User

D-Rails crawling in process ... crawling failedTime limit:MS Memory Limit:10000KB 64bit IO Format: %I64D &%i64u SubmitStatus Practice POJ 1363Appoint Description:System Crawler (2016-05-09)

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 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 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

Read the question for a long time ...

Maybe test instructions is asking if you can output the sequence you entered in 1.......N order.

#include <stdio.h> #include <stack>using namespace Std;int main () {int N;int num[1005];while (~scanf ("%d", &n) &&n) {while (true) {int x;scanf ("%d", &x), if (x==0) break;else{num[0]=x;for (int i=1;i<n;i++) scanf ("%d", &num[i]);} Stack<int>s;int t=0;for (int i=0;i<n;i++) {s.push (i+1); while (!s.empty () &&s.top () ==num[t]) {s.pop () ; t++;}} if (t==n) puts ("Yes"), Elseputs ("No"), while (!s.empty ()) S.pop ();} printf ("\ n");} return 0;}


poj1363 Rails (stack emulation)

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.