Little Elephant and Permutations
All submissions for this problem are available.
The Little Elephant likes permutations. This time he has a permutation A, A, ..., 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.
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, A, ..., A[N].
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.
1 ≤ T ≤ 474
1 ≤ N ≤ 100
It is guaranteed that the sequence A, A, ..., A[N] is a permutation of numbers 1, 2, ..., N.
Input: 4 1 1 2 2 1 3 3 2 1 4 1 3 2 4 Output: YES YES NO YES
Case 1. Here N = 1, so we have no pairs (i; j) with 1 ≤ i < j ≤ N. 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 < j ≤ N, the pair (1; 2). Since A = 2 and A = 1 then A > A 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 < N (the value i = 1) and A[i] > A[i+1] for this value of i since A > A. Hence this permutation is also good.
Case 3. Here N = 3, and we have three pairs (i; j) with 1 ≤ i < j ≤ N. We have A = 3, A = 2, A = 1. Hence A > A, A > A and A > A. So the number of inversions is equal to 3. To count the number of local inversion we should examine inequalities A > A and A > A. 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 = 3 > 2 = A. 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.
|Tags||ad-hoc, cakewalk, cook28, witua|
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, PYP3, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.