STPAR - Street Parade

For sure, the love mobiles will roll again on this summer's street parade. Each year, the organisers decide on a fixed order for the decorated trucks. Experience taught them to keep free a side street to be able to bring the trucks into order.

The side street is so narrow that no two cars can pass each other. Thus, the love mobile that enters the side street last must necessarily leave the side street first. Because the trucks and the ravers move up closely, a truck cannot drive back and re-enter the side street or the approach street.

You are given the order in which the love mobiles arrive. Write a program that decides if the love mobiles can be brought into the order that the organisers want them to be.


There are several test cases. The first line of each test case contains a single number n, the number of love mobiles. The second line contains the numbers 1 to n in an arbitrary order. All the numbers are separated by single spaces. These numbers indicate the order in which the trucks arrive in the approach street. No more than 1000 love mobiles participate in the street parade. Input ends with number 0.


For each test case your program has to output a line containing a single word "yes" if the love mobiles can be re-ordered with the help of the side street, and a single word "no" in the opposite case.


Sample input:
5 1 2 4 3 

Sample output:


The sample input reflects the following situation:

The five trucks can be re-ordered in the following way:

hide comments
ckanzz: 2020-07-20 17:14:49

Good stack question but poor instructions, if code doesn't work try the test cases in the comments especially:
1 2 10 5 4 3 7 6 8 9
(Have to pop the stack at 7 once, figure out why and you'll crack it)
Thanks, everyone in the comments who provided the test cases

s_tank00_: 2020-07-15 11:43:59

give me a test a case for no

subhram79788: 2020-07-07 09:48:04

can anyone explain to me the doubt->can we exit the vehicle from the right horizontal street?

moaaz_mahmoud: 2020-07-06 20:20:08

Can anyone tell me what's wrong with this approach? It has passed all of the test cases I've seen but gets TLE when submitted.

The main street is represented by a queue and the side street is represented by a stack. All of the cars are initially in the queue.
Look for car 1 (next = 1)
As long as there are cars in the main street:
If there is a car in the side street, and it is the next one we are looking for, let it pass and look for the next one.
That is, pop the stack and increment next.
Else if the car in the front of the queue is the next, let it pass and look for the next one. That is, pop the queue
and increment next.
Else Move the car in the front of the queue to the top of the stack.
//At this point, the queue is empty and the stack is not.
Examine the cars left on the stack one by one. If the top is the next one, let it pass, and if not, the answer is "no". If the stack eventually gets empty, the answer is "yes".

kumar_anubhav: 2020-06-26 13:06:36

AC in one Go!! :)

spectum: 2020-06-11 08:13:31

A classic question yet poorly described
That 0 input signifies that you should run the code until 0 encounters, means for ex:-
5 1 2 4 3
1 2
3 1 2
0 -> here you should stop
thanks comment section @_-

shreya_deep_7: 2020-06-04 08:43:54

So confusing problem statement

avi_kumar15: 2020-05-19 14:54:07

nice prob, this test case saved me
1 2 10 5 4 3 7 6 8 9
thanks @warri00r

Last edit: 2020-05-19 14:54:24
dnkm: 2020-05-11 19:51:55

I was wondering why they bothered putting 0 at the end.... I realize after getting two WAs lol...

sattu_beginner: 2020-04-16 14:00:51

finally Got AC !
just simple use of STACK!

Added by:Patryk Pomykalski
Time limit:1s-2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:Swiss Olympiad in Informatics 2004