The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numbers 1, 2, ..., N.

He calls a permutation A good, if the number of its inversions is equal to the number of its local inversions. The number of inversions is equal to the number of pairs of integers (i; j) such that 1 ≤ i < j ≤ N and A[i] > A[j], and the number of local inversions is the number of integers i such that 1 ≤ i < N and A[i] > A[i+1].

The Little Elephant has several such permutations. Help him to find for each permutation whether it is good or not. Print YES for a corresponding test case if it is good and NO otherwise.

------ Input ------ 

The first line of the input contains a single integer T, the number of test cases. T test cases follow. The first line of each test case contains a single integer N, the size of a permutation. The next line contains N space separated integers A[1], A[2], ..., A[N].

------ Output ------ 

For each test case output a single line containing the answer for the corresponding test case. It should be YES if the corresponding permutation is good and NO otherwise.

------ Constraints ------ 

1 ≤ T ≤ 474 

1 ≤ N ≤ 100 

It is guaranteed that the sequence A[1], A[2], ..., A[N] is a permutation of numbers 1, 2, ..., N.

----- Sample Input 1 ------ 
4
1
1
2
2 1
3
3 2 1
4
1 3 2 4
----- Sample Output 1 ------ 
YES
YES
NO
YES
----- explanation 1 ------ 

Case 1. Here N = 1, so we have no pairs (i; j) with 1 ? i . So the number of inversions is equal to zero. The number of local inversion is also equal to zero. Hence this permutation is good.

Case 2. Here N = 2, and we have one pair (i; j) with 1 ? i , the pair (1; 2). Since A[1] = 2 and A[2] = 1 then A[1] > A[2] and the number of inversions is equal to 1. The number of local inversion is also equal to 1 since we have one value of i for which 1 ? i  (the value i = 1) and A[i] > A[i+1] for this value of i since A[1] > A[2]. Hence this permutation is also good.

Case 3. Here N = 3, and we have three pairs (i; j) with 1 ? i . We have A[1] = 3, A[2] = 2, A[3] = 1. Hence A[1] > A[2], A[1] > A[3] and A[2] > A[3]. So the number of inversions is equal to 3. To count the number of local inversion we should examine inequalities A[1] > A[2] and A[2] > A[3]. They both are satisfied in our case, so we have 2 local inversions. Since 2 ? 3 this permutations is not good.

Case 4. Here we have only one inversion and it comes from the pair (2; 3) since A[2] = 3 > 2 = A[3]. This pair gives also the only local inversion in this permutation. Hence the number of inversions equals to the number of local inversions and equals to one. So this permutation is good.

Difficulty: EASY
Tags: Sorting

Complete search
Skill types: Sorting

Complete search

Starter code:

Solutions:
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	c1 = c2 = k = 0
	for i in range(n - 1):
		if A[i] > A[i + 1]:
			c1 += 1
	for i in range(n - 1):
		for j in range(i + 1, n):
			if A[i] > A[j]:
				c2 += 1
	if c1 == c2:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(c, c1) = (0, 0)
	for j in range(n):
		for k in range(j):
			if l[k] > l[j]:
				c = c + 1
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			c1 = c1 + 1
	if c == c1:
		print('YES')
	else:
		print('NO')


a = int(input(''))
for i in range(a):
	x = int(input(''))
	v = [int(x) for x in input('').split()]
	l = list(v)
	c = 0
	d = 0
	for j in range(0, len(l) - 1):
		if l[j] > l[j + 1]:
			c = c + 1
	for k in range(0, len(l)):
		for kk in range(k, len(l)):
			if l[k] > l[kk]:
				d = d + 1
	if c == d:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	local = 0
	inver = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			local += 1
	for i in range(n - 1):
		for j in range(i + 1, n):
			if a[i] > a[j]:
				inver += 1
	print('YES' if local == inver else 'NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(inh, loc) = (0, 0)
	for i in range(n - 1):
		for j in range(i + 1, n):
			if a[i] > a[j]:
				inh += 1
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			loc += 1
	if inh == loc:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(x, y) = (0, 0)
	for i in range(n - 1):
		for j in range(i + 1, n):
			if a[i] > a[j]:
				x += 1
		if a[i] > a[i + 1]:
			y += 1
	if x == y:
		print('YES')
	else:
		print('NO')


def is_good_permutation(n, a):
	inversions = 0
	local_inversions = 0
	for i in range(n):
		for j in range(i + 1, n):
			if a[i] > a[j]:
				inversions += 1
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			local_inversions += 1
	return inversions == local_inversions
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if is_good_permutation(n, a):
		print('YES')
	else:
		print('NO')


t = int(input())
for ni in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	inv = 0
	linv = 0
	for i in range(n):
		for j in range(i):
			if a[j] > a[i]:
				inv = inv + 1
		if i < n - 1:
			if a[i] > a[i + 1]:
				linv = linv + 1
	if inv == linv:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	inversion = False
	for i in range(n - 2):
		for j in range(i + 2, n):
			if a[i] > a[j]:
				inversion = True
				break
	if inversion == False:
		print('YES')
	else:
		print('NO')


cc = int(input())
for i in range(cc):
	n = int(input())
	a = [int(i) for i in input().split()]
	count1 = 0
	count2 = 0
	for i in range(n):
		for j in range(i + 1, n):
			if a[i] > a[j]:
				count1 += 1
	for k in range(1, n):
		if a[k] < a[k - 1]:
			count2 += 1
	if count1 == count2:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	count1 = 0
	count2 = 0
	for i in range(0, n - 1):
		if l[i] > l[i + 1]:
			count1 += 1
	for i in range(0, n - 1):
		for j in range(i + 1, n):
			if l[i] > l[j]:
				count2 += 1
	if count1 == count2:
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	inv = 0
	lci = 0
	for i in range(0, n - 1):
		for j in range(i + 1, n):
			if l[i] > l[j]:
				inv += 1
	for i in range(0, n - 1):
		if l[i] > l[i + 1]:
			lci += 1
	if inv == lci:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	g_inv = 0
	l_inv = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			l_inv += 1
	for i in range(n - 1):
		for j in range(i + 1, n):
			if a[i] > a[j]:
				g_inv += 1
	if g_inv == l_inv:
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	inversion = 0
	L_inversion = 0
	for i in range(N - 1):
		if A[i] > A[i + 1]:
			L_inversion += 1
		for j in range(i + 1, N):
			if A[i] > A[j]:
				inversion += 1
	if inversion == L_inversion:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	msn = int(input())
	msl = list(map(int, input().split()))
	c = 0
	vc = 0
	for i in range(msn - 1):
		if msl[i] > msl[i + 1]:
			c += 1
	for j in range(msn):
		for jj in range(j, msn):
			if msl[j] > msl[jj]:
				vc += 1
	if c == vc:
		print('YES')
	else:
		print('NO')


t = input()
for c in range(0, int(t)):
	n = int(input())
	l = list(map(int, input().split()))
	local = 0
	for j in range(n - 1):
		if l[j] > l[j + 1]:
			local += 1
	final = 0
	j = 0
	while j < 100:
		if j == n:
			break
		k = j + 1
		while k < 100:
			if k > n - 1:
				break
			if l[j] > l[k]:
				final += 1
			k += 1
		j += 1
	print('YES' if local == final else 'NO')


import math
t = int(input())
l = []
for _ in range(t):
	local = 0
	g = 0
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			local += 1
	for i in range(n - 1):
		for j in range(i + 1, n):
			if arr[i] > arr[j]:
				g += 1
	if local == g:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ins = 0
	for i in range(len(a)):
		for j in range(i, len(a)):
			if a[i] > a[j]:
				ins += 1
	lins = 0
	for i in range(len(a) - 1):
		if a[i] > a[i + 1]:
			lins += 1
	print('YES' if ins == lins else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	count = 0
	for i in range(len(a) - 1):
		for j in range(i + 1, len(a)):
			if a[i] > a[j]:
				count += 1
	sum1 = 0
	for i in range(1, len(a)):
		if a[i - 1] > a[i]:
			sum1 += 1
	if count == sum1:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	s = int(input())
	a = list(map(int, input().split()))
	x = 0
	y = 0
	for i in range(s):
		for j in range(i + 1, s):
			if a[i] > a[j]:
				x += 1
	for k in range(s - 1):
		if a[k] > a[k + 1]:
			y += 1
	if x == y:
		print('YES')
	else:
		print('NO')


op = int(input())
for i in range(op):
	n = int(input())
	c = 0
	d = 0
	a = [int(n) for n in input().split()]
	for j in range(n - 1):
		if a[j] > a[j + 1]:
			c = c + 1
	for k in range(n):
		for l in range(k + 1, n):
			if a[k] > a[l]:
				d = d + 1
	if c == d:
		print('YES')
	else:
		print('NO')


from itertools import permutations
for tc in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	inv = [a[i] > a[j] for (i, j) in permutations(range(n), 2) if i < j].count(True)
	linv = len([i for i in range(n - 1) if a[i] > a[i + 1]])
	if inv == linv:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 'YES'
	for x in range(n - 1):
		count = 0
		local_inversions = 0
		if arr[x] > arr[x + 1]:
			local_inversions = 1
		for y in range(x + 1, n):
			if arr[x] > arr[y]:
				count += 1
		if count != local_inversions:
			ans = 'NO'
			break
	print(ans)


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	excpt = 0
	for i in range(N - 2):
		if excpt:
			break
		a = A[i]
		for j in range(i + 2, N):
			if a > A[j]:
				excpt = 1
				break
	print('NO') if excpt else print('YES')


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		i = l = 0
		for j in range(n):
			for k in range(j + 1, n):
				if a[j] > a[k]:
					i += 1
		for j in range(n - 1):
			if a[j] > a[j + 1]:
				l += 1
		if i == l:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(local, oof) = (0, 0)
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			local += 1
	for i in range(n):
		for j in range(i + 1, n):
			if arr[i] > arr[j]:
				oof += 1
	if oof == local:
		print('YES')
	else:
		print('NO')


T = int(input())
while T > 0:
	N = int(input())
	arr = list(map(int, input().split()))
	x = y = 0
	for i in range(N - 1):
		if arr[i] > arr[i + 1]:
			x += 1
		for j in range(i + 1, N):
			if arr[i] > arr[j]:
				y += 1
	if x == y:
		print('YES')
	else:
		print('NO')
	T -= 1


for i in range(int(input())):
	n = int(input())
	lc = 0
	inc = 0
	lis = list(map(int, input().split()))
	for i in range(n):
		j = i + 1
		while j != n:
			if lis[i] > lis[j]:
				lc += 1
			j += 1
		if i + 1 < n and lis[i] > lis[i + 1]:
			inc += 1
	if inc == lc:
		print('YES')
	else:
		print('NO')


tests = int(input())
for i in range(tests):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 'YES'
	for x in range(n - 1):
		count = 0
		local_inversions = 0
		if arr[x] > arr[x + 1]:
			local_inversions = 1
		for y in range(x + 1, n):
			if arr[x] > arr[y]:
				count += 1
		if count != local_inversions:
			ans = 'NO'
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(inv, li) = (0, 0)
	for i in range(n):
		for j in range(i + 1, n):
			if a[i] > a[j]:
				inv += 1
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			li += 1
	if inv == li:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(local, main) = (0, 0)
	for j in range(n - 1):
		if a[j] > a[j + 1]:
			local = local + 1
		for k in range(j + 1, n):
			if a[j] > a[k]:
				main = main + 1
	if local == main:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(1, t + 1):
	n = int(input())
	l = list(map(int, input().split()))
	ans = False
	for i in range(n - 1):
		for j in range(i + 2, n):
			if l[i] > l[j]:
				ans = True
				break
	if not ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c1 = 0
	c2 = 0
	for i in range(0, n - 1):
		for j in range(i + 1, n):
			if a[i] > a[j]:
				c1 += 1
	for i in range(0, n - 1):
		if a[i] > a[i + 1]:
			c2 += 1
	if c1 == c2:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	lis = list(map(int, input().split()))
	inv = 0
	l_inv = 0
	for i in range(n - 1):
		if lis[i] > lis[i + 1]:
			inv += 1
			l_inv += 1
		for j in range(i + 2, n):
			if lis[i] > lis[j]:
				inv += 1
	if l_inv == inv:
		print('YES')
	else:
		print('NO')


def count_inversions(arr):
	if len(arr) <= 1:
		return (arr, 0)
	mid = len(arr) // 2
	(left, inv_left) = count_inversions(arr[:mid])
	(right, inv_right) = count_inversions(arr[mid:])
	(merged, inv_merge) = merge(left, right)
	return (merged, inv_left + inv_right + inv_merge)

def merge(left, right):
	result = []
	inversions = 0
	i = j = 0
	while i < len(left) and j < len(right):
		if left[i] <= right[j]:
			result.append(left[i])
			i += 1
		else:
			result.append(right[j])
			j += 1
			inversions += len(left) - i
	result += left[i:]
	result += right[j:]
	return (result, inversions)

def is_good_permutation(arr):
	inversions = count_inversions(arr)[1]
	local_inversions = sum((1 for i in range(len(arr) - 1) if arr[i] > arr[i + 1]))
	if inversions == local_inversions:
		print('YES')
	else:
		print('NO')
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	is_good_permutation(l)


T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	inversions = 0
	local_inversions = 0
	for i in range(N - 1):
		if A[i] > A[i + 1]:
			local_inversions = local_inversions + 1
	for i in range(N - 1):
		for j in range(i + 1, N):
			if A[i] > A[j]:
				inversions = inversions + 1
	if inversions == local_inversions:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	z = 0
	m = 0
	for i in range(1, n):
		for j in range(i, n):
			if lis[i - 1] > lis[j]:
				z = z + 1
		if lis[i - 1] > lis[i]:
			m = m + 1
	if z == m:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		(lc, inverse) = (0, 0)
		for i in range(1, n):
			if a[i - 1] > a[i]:
				lc = lc + 1
		for i in range(0, n - 1):
			for j in range(i + 1, n):
				if a[i] > a[j]:
					inverse = inverse + 1
		if inverse == lc:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	(l, t) = (0, 0)
	for i in range(n - 1):
		c = a[i]
		l += int(c > a[i + 1])
		t += sum([int(c > a[j]) for j in range(i + 1, n)])
	if l == t:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	ans = 'YES'
	for i in range(N):
		if A[i] > i + 2 or A[i] < i:
			ans = 'NO'
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	inv = 0
	linv = 0
	for i in range(n):
		for j in range(n):
			if i < j and A[i] > A[j]:
				inv += 1
	for i in range(1, n):
		if A[i - 1] > A[i]:
			linv += 1
	print('YES' if inv == linv else 'NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 'YES'
	for i in range(n):
		if a[i] > i + 2 or a[i] < i:
			ans = 'NO'
			break
	print(ans)


def selectionsort(L):
	inver = 0
	locinver = 0
	for i in range(len(L)):
		for j in range(i + 1, len(L)):
			if L[i] > L[j]:
				inver += 1
				if j == i + 1:
					locinver += 1
	return (inver, locinver)
t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	(inver, locinver) = selectionsort(l)
	if inver == locinver:
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	(d, e) = (0, 0)
	for j in range(n):
		for k in range(j + 1, n):
			if a[j] > a[k]:
				d += 1
	for l in range(n - 1):
		if a[l] > a[l + 1]:
			e += 1
	if d == e:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	inversions = 0
	local_inversions = 0
	for i in range(n - 1):
		for j in range(i + 1, n):
			if arr[i] > arr[j]:
				inversions += 1
	for i in range(n - 1):
		if arr[i] > arr[i + 1]:
			local_inversions += 1
	if inversions == local_inversions:
		print('YES')
	else:
		print('NO')


t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	d = 0
	for j in range(n):
		for k in range(j + 1, n):
			if a[j] > a[k]:
				c += 1
	for j in range(n - 1):
		if a[j] > a[j + 1]:
			d += 1
	if c == d:
		print('YES')
	else:
		print('NO')


def way(s, l, c, n, h):
	if len(l) == n:
		f = 0
		for i in range(n):
			for j in range(i + 1, n):
				if l[i] > l[j]:
					f += 1
		f2 = 0
		i = 0
		while i + 1 < n:
			if l[i] > l[i + 1]:
				f2 += 1
			i = i + 1
		if f2 == f:
			p = l.copy()
			h.append(p)
	else:
		for i in range(0, n):
			if c[i] == 1:
				continue
			c[i] = 1
			l.append(str(s[i]))
			way(s, l, c, n, h)
			c[i] = 0
			l.pop()
T = int(input())
for i in range(T):
	n = int(input())
	l = [int(j) for j in str(input()).split(' ')]
	f = 0
	for i in range(n):
		for j in range(i + 1, n):
			if l[i] > l[j]:
				f += 1
	f2 = 0
	i = 0
	while i + 1 < n:
		if l[i] > l[i + 1]:
			f2 += 1
		i = i + 1
	if f2 == f:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	little = int(input())
	elephant = [int(elephant) for elephant in input().split()]
	count = 0
	flag = 0
	for i in range(little - 1):
		if elephant[i] > elephant[i + 1] and i < little:
			count += 1
		for j in range(i + 1, little):
			if elephant[i] > elephant[j]:
				flag += 1
	if count == flag:
		print('YES')
	else:
		print('NO')