wonderful-sequence-3

A sequence of integers is called a wonderful sequence if all the integers in it are positive and it is a strictly increasing sequence.

Given a sequence of integers, you have to make it a wonderful sequence. For that you can change any element you want, but you should make as less changes as possible in order to make it a wonderful sequence.

Input

The first line of input is an integer T(T ≤ 5), the number of test cases. Each test case contains 2 lines.

The first line of the test case contains an integer (0 < N ≤ 100000), i.e. the number of elements in the original sequence.

The second line contains N positive integers, no larger than 2000000000, which forms the original sequence.

Output

For each test case output the minimal number of elements you must change in the original sequence to make it a wonderful sequence.

SAMPLE INPUT
3
2
1 2
3
3 2 1
5
10 5 6 7 8

SAMPLE OUTPUT
0
2
1

Explanation

For the 1st test case you needn't to change any elements.
For the 2nd test case you can change 3 into 1 and change 1 into 3.
For the 3rd test case you can change 10 into 1.

Source: hackerearth | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
from sys import stdin
def subsequence(seq):
	if not seq:
		return seq
 
	M = [None] * len(seq)    # offset by 1 (j -> j-1)
	P = [None] * len(seq)
 
	# Since we have at least one element in our list, we can start by 
	# knowing that the there's at least an increasing subsequence of length one:
	# the first element.
	L = 1
	M[0] = 0
 
	# Looping over the sequence starting from the second element
	for i in range(1, len(seq)):
		# Binary search: we want the largest j <= L
		#  such that seq[M[j]] < seq[i] (default j = 0),
		#  hence we want the lower bound at the end of the search process.
		lower = 0
		upper = L
 
		# Since the binary search will not look at the upper bound value,
		# we'll have to check that manually
		if seq[M[upper-1]] < seq[i]:
			j = upper
 
		else:
			# actual binary search loop
			while upper - lower > 1:
				mid = (upper + lower) // 2
				if seq[M[mid-1]] < seq[i]:
					lower = mid
				else:
					upper = mid
 
			j = lower    # this will also set the default value to 0
 
		P[i] = M[j-1]
 
		if j == L or seq[i] < seq[M[j]]:
			M[j] = i
			L = max(L, j+1)
 
	# Building the result: [seq[M[L-1]], seq[P[M[L-1]]], seq[P[P[M[L-1]]]], ...]
	result = []
	pos = M[L-1]
	for _ in range(L):
		result.append(seq[pos])
		pos = P[pos]
 
	return result[::-1]
t = stdin.readline()
t = int(t)
for jj in range(t):
	n = int(stdin.readline())
	a = list(map(int,stdin.readline().strip().split()))
	b =  subsequence(a)
	print(n - len(b))