GONESORT - G-One Sort

no tags 

After Killing RaOne G-One had nothing to do, so he started reading books and became an avid book reader.

To avoid purchasing books he started working in a library.

Every evening he had to arrange the books on the shelf in increasing order of their serial number.

Every book in the library is numbered.

..

G-One found an ingenious way of arranging the books.  He can remove any book from the shelf and put it either at the beginning or at the end of the shelf.

For example if the books are arranged in the order below:

    2  3  1  7  4  5  6

he can make it sorted by removing '1' and placing it at the beginning and then removing '7' and placing it in the end.

Since the book shelf can be very big and can have a large number of books, he needs your help to tell him the minimum remove and place operations he needs to do.

Can you help him?

Input

1st line of the input contains number 't' denoting the number of shelves in the library. 2*t lines follow this

1st line of each test case will have single number 'b' - denoting number of books on the shelf.

2nd line will contain b numbers, each bi denoting the serial number of the book.

Output

For each test case output a single integer denoting the minimum number of remove and place operations needed to arrange the shelf.

Example

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

Output:
2
0
5

NOTE All the values will be in the range [0, 107], number of test cases ≤ 100.


hide comments
Karthikeyan R: 2012-05-27 09:39:41

Test cases are so weak. The answer for 2 3 1 7 5 6 is 2. If the numbers are not in permutations then the answer is 2. Wrong solutions gets accepted. Check it.

Radhakrishnan Venkataramani: 2012-05-27 06:14:35

What is the sol for this case
6
2 3 1 7 5 6

!nvincible: 2012-05-20 21:29:24

O(nlog(n)) solution is accepted. But problem setters should clearly define the range as b < 500000 also get accepted.

:D: 2012-05-20 09:16:41

Got 0.04 AC with O(N*log(N)), random access iterating through an array and scanf. There really shouldn't be any problems with TLE in C++.

teoy: 2012-04-28 16:17:34

my o(nlogn) algorithm got TLE ~,what is the range of n……
Are the numbers must be the permutation of 1 to n?


----
n ranges from 1 to 10^7
NO the numbers are not permutation of 1 to n

Last edit: 2012-05-09 09:22:42
bashrc is back: 2012-04-25 12:31:34

b is definitely much much less than 10^7.@problem setter constraints must be mentioned clearly.
My o(n) solution takes 0.14s while o(nlogn) takes 0.00?After scrutinizing my code i am convinced it is o(n)..Very weird...O_o

Mitch Schwartz: 2012-04-10 02:14:27

@ebd: The problem statement was altered after I wrote the comment. I hadn't noticed, so thanks for mentioning it.

Last edit: 2012-04-10 02:20:24
ebd: 2012-04-10 00:30:40

@Mitch: Test data seems to be correct and to conform an input specification. I think there's no need to rejudge. Just don't assume things that are not mentioned in the problem statement.

Mitch Schwartz: 2012-04-07 03:42:32

It shouldn't be hard to generate input data that conforms to problem specifications. I generally prefer Python; for example, here you can use range() then shuffle() then print/join().. the ability to write it in just a few lines reduces chance for bugs. I'll wait for rejudge.

Siarhei Kulik: 2012-04-07 03:42:32

By asserts I have figured out that there is a case when the number of a book doesn't belong to [1; N]. Please, check your test data.
Also, the best solution worked for 0.01 sec. so I don't believe that there is a case with 10^7 numbers... even to read 100000 numbers in C++ in 0.01 secound seems a quite impossible...
UPD: OK, I've got AC. The trick is that the numbers are actually don't belong to [1; N] so you have to renumber them in such a way (at least if your solution is similiar to mine :) ).

Last edit: 2012-04-06 16:32:41

Added by:Devil D
Date:2012-04-05
Time limit:0.100s-1s
Source limit:20000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own