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

pawone3: 2018-01-18 11:16:45

Last edit: 2018-01-18 11:18:41
lucifer1704: 2018-01-17 10:39:22

Wrong Answer..
every test case worked.. even
6 1 5 2 4 3 7 10 8 9..

harismuzaffer: 2018-01-13 07:09:20

how many test cases are there?

manjeet_: 2018-01-01 10:30:17

Thanks @r15habhgup11 .... ur test case helps me alot

anurag_tangri: 2017-12-24 11:03:07

just use one stack and a array or vector for final result. Simple but time consuming implementation problem

chandyshot: 2017-11-18 06:43:31

can some one clarify this question :
1) The number on trucks are unique lies in range 1 to n
2) The order must be increasing or decreasing or any of them would work

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