LUCISORT - Lucifer Sort

no tags 

Lucifer is as bored as G-One after the defeat of Raone. he has no computer game to play. He Like g-One started reading books and Unlike G-One he bought a big book shelf and lots of books. He labelled all books with serial numbers. (All books have different serial numbers).

He invites his friends and small kids to home and allows them to read books. But the problem is everyone replaces the books anywhere on the shelf.

At the end of the day Lucifer has to sort all the books in increasing order of serial number on the shelf from left to right. The problem is he knows just one way of sorting called LUCIFER SORT.

He can pick a book from anywhere on the shelf but can replace it only in the center of the remaining books.

For example of the books are in order: 2 1 3 4 5 6

The steps of sorting are

  • 1 3 4 2 5 6  -  Pick 2 and place between between 4 and 5 in (1 3 4 5 6)
  • 1 4 2 3 5 6  -  Pick 3 and place between between 2 and 5 in (1 4 2 5 6)
  • 1 2 3 4 5 6  -  Pick 4 and place between between 3 and 5 in (1 2 3 5 6)

Assuming positions are numbered from 1 to N.

While replacingm, if number of books left is even then it is put back between n/2 and n/2+1 position. If the number of books left is odd, it is put back between (n+1)/2 and (n+1)/2+1 position.

Since the number of books are large, he needs your help to tell me number of steps he needs to sort the shelf.

Input

First line contains number of shelves.

For each shelf there are 2 lines:

First line will have number of books.

Second line will have the serial number of the books at the end of the day.

Output

Single line telling how many books he needs to remove and replace.

If there is no way he can sort the books by this process Print "YOU ARE DOOMED" without the quotes.

Example

Input:
3
6
1 2 3 4 5 6
6
2 1 3 4 5 6
6
6 5 4 3 2 1

Output:
0
3
6

All the values will be in the range [0, 10^7]
number of test cases <= 100.


hide comments
Oleg: 2024-01-17 05:59:43

There are no more than 100 books on a shelf.
(100K should be also solvable :) )

Rishabh singh: 2013-10-20 20:02:13

the number of books on a shelf will be within 100 or 10^7????

Last edit: 2013-10-20 20:02:34
Alex Anderson: 2012-12-12 21:08:04

My AC program gets 15 for DevilD's size 28 case, 3 for his size 7 case.
5 for Vimal's size 8 case.

Vimal poonia: 2012-05-16 15:01:11

@D

I think answer should be 15,
1 for moving last 5733 to middle and then all element that are ahead of it
1+14
15

One more example
8
4 5 6 7 8 9 10 3
answer will be 5 not 4

Last edit: 2012-05-16 15:33:26
Devil D: 2012-05-16 10:00:14

Some test cases
28
5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5733

answer is 14

-----------------
7
4 3 2 1 5 6 7

answer is 3

maaz: 2012-04-10 15:05:35

Thats what u say a gud question..


Added by:Devil D
Date:2012-04-06
Time limit:1s
Source limit:15000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Lucifer Sort