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.

Input

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.

Output

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.

Example

Sample input:
5
5 1 2 4 3 
0

Sample output:
yes

Illustration

The sample input reflects the following situation:

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


hide comments
byte_rider12: 2018-05-13 05:52:30

I have a doubt?
Can we place a truck from the side lane back into the left side of the main road ? Also can we place a truck back from the right lane ?
Can anyone please help me with this ?

trupti18: 2018-05-05 15:18:57

I had 3 WAs because of this Test Case:
5
5 3 2 1 5 4
0
Above test case should print yes.
Second line of each test case (that represents the sequence in which love mobiles arrive ) can have few love mobile repeated and hence the sequence may not be of length that is equal to total number of love mobiles.
In the above case Total number of love mobiles = 5 but the length of their arrival sequence (5 3 2 1 5 4) is 6.

Ishan: 2018-04-18 17:02:38

My assumption was wrong, you can not take top out of the main street and put to top if sidelane but you can do the reverse all the time.

Last edit: 2018-04-18 17:03:23
Ishan: 2018-04-18 15:59:13

Why is
4
4 2 3 1
no
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:
8
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
5 3 2 1 4
yes

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

Added by:Patryk Pomykalski
Date:2004-07-01
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