Problem
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 1:
4 1 1 2 2 1 3 3 2 1 4 1 3 2 4
YES YES NO YES
Explanation:
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[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 < N (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 < j ? N. 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. /* package codechef; // don't place package name! */
Program :
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner in=new Scanner(System.in);
int t=in.nextInt();
for(int p=1;p<=t;p++)
{
int n=in.nextInt();
int a[]=new int[n];
int c=0,d=0;
for(int i=0;i<n;i++){
a[i]=in.nextInt();
}
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])
c++;
}
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(a[i]>a[j])
d++;
}
}
if(c==d)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
0 Comments