Summer Camp (1) Fifth bullet-----Rails (Uva514)

Source: Internet
Author: User

The Poppush city has a famous railway station. The country is full of hills. And this train station was built in the last century. Unfortunately, there was limited funding at the time. So we can only build a road track. Moreover, this causes the station to be put into use only one track at a time, because it lacks space and two trains will have no way to go. Concrete look.

  The local practice is that each train moves from direction A to B, and in some way it reorganizes the carriages. Suppose the train is going to reach a, with n carriages (n<=1000), which are labeled 1 to N in ascending order. In charge of the leadership from the group of carriages, it must be known whether the car can be pulled out of the B, and this reorganization of the sequence is a1\a2\a3...an. Help him and write a procedure to determine if it is possible to follow the order of the carriages required. You can assume that a single compartment can be separated from the train before they enter the platform. And they can move freely, knowing they're on the B track. You can also assume that the platform can drop countless carriages at any time. But as soon as a compartment enters the platform, it cannot return to the a track, and if it leaves the platform and drives to track B, it cannot return to the platform.
Input: This input file consists of multiple line blocks. Each block describes a sequence of multiple requirements for a reorganized compartment. The first line in each block is an integer n, which is used to indicate the number of carriages in each row above. This fast last line is just a number 0 to mark the end of the fast the last block is just a 0 exclusive row.   Output: This output file contains more than one row, which corresponds to the number of rows in the carriage one by one. If the alignment is feasible, the output is yes, otherwise the output is no. In addition there is an empty line behind each corresponding block. The output file does not exist in response to the last what data does not have.   Output 5 1 2 3) 4 5 5 4 1) 2 3 0 6 6 5 4 3 2 1 0 0   Output Yes No   Yes
Problem Analysis: Transit C, carriages conform to the principle of LIFO, you can use the stack to solve the problem, it is worth noting that to enter the possible outbound order, rather than the stack order.
1#include <cstdio>2#include <iostream>3#include <stack>4 using namespacestd;5 intn,t[1005];6 intMain ()7 {8    while(Cin>>n &&N)9  {Tenstack<int>s; One    intA,b,flag; A     while(cin>>t[1]) -     { -a=b=flag=1; the       if(t[1] ==0) -        { -cout<<Endl; -           Break; +        } -        for(intI=2; i<=n;i++) +scanf"%d",&t[i]); A        while(b<=N) at       { -         if(a==T[b]) -            { -a++; -b++; -            } in         Else if(!s.empty () &&s.top () = =T[b]) -            { to S.pop (); +b++; -            } the         Else if(a<=N) *S.push (a++); $         ElsePanax Notoginseng             { -flag=0; the                  Break; +             } A        } theprintf"%s\n", Flag?"Yes":"No"); +    } -  } $     return 0; $}
View Code

Summer Camp (1) Fifth bullet-----Rails (Uva514)

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.