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
Ishan: 2018-04-18 15:59:13

Why is
4 2 3 1
I can see it is possible. Here are my steps
first put 4 to sidelane
then push 2 forward on main street
then put 3 to sidelane
then take 2 out of main and put it to sidelane
then put 1 to main street.
now side has 2,3,4 in reverse order just put them after 1 already in main.

Last edit: 2018-04-18 16:01:10
naruto17: 2018-04-03 16:19:00

@pkdhirasaria , thanks for help :)

prashant_1098: 2018-03-24 16:42:29

AC in one go!!!

monish007: 2018-03-16 22:35:08

Weak Test Cases:
1 4 2 8 6 5 7 3
Should Give WA but my code got accepted

Last edit: 2018-03-16 22:35:37
mohit1069: 2018-03-16 00:05:21

just use 1 array as a stack 0.00 secs sol accepted

Last edit: 2018-03-16 00:20:31
zephyr_96: 2018-02-25 21:56:35

Test cases in the comments helped me a lot. Look out for
5 3 2 1 4

kishankg: 2018-02-17 23:39:17

Thanks guys. You helped a lot in understanding the problem like the 0 input after all the test cases not after each test case.
Finally solved it.

Last edit: 2018-02-17 23:41:02
vivekpandit: 2018-02-11 21:54:02

AC in one go

pkdhirasaria: 2018-01-23 10:43:57

easy problem...solved in one go :-D
just used one stack that's all no array nothing
some test case :
6 1 5 2 7 3 4 10 8 9
1 3 4 2 5
4 2 3 1
1 2 10 5 4 3 9 8 7 6
4 1 5 3 2
3 1 2 5 4

veena_7: 2018-01-19 08:25:46

my first problem in spoj accepted in one go!thanks to the comments..helped me in understanding the problem more clearer..

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