The Little Elephant likes permutations. This time he has a permutation A[1]A[2], ..., A[N] of numbers 12, ...,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 (ij) 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 beYES 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 12, ..., N.

Example

Input:
4
1
1
2
2 1
3
3 2 1
4
1 3 2 4 Output:
YES
YES
NO
YES

总结一下有下面关系:

全局逆序数 == 起泡排序交换数据的交换次数

本题数据量小,能够使用暴力法计算。

可是这样时间效率是O(N*N),要想减少到O(nlgn)那么就要使用merger sort。

以下一个类中暴力法和merge sort方法都包含了。

#pragma once
#include <stdio.h>
#include <stdlib.h> class LittleElephantAndPermutations
{
int localInverse(const int *A, const int n)
{
int ans = 0;
for (int i = 1; i < n; i++)
{
if (A[i-1] > A[i]) ans++;
}
return ans;
} int globalInverse(const int *A, const int n)
{
int ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (A[i] > A[j]) ans++;
}
}
return ans;
} int *mergeArr(int *left, int L, int *right, int R, int &c)
{
int *lr = new int[L+R];
int i = 0, j = 0, k = 0;
while (i < L && j < R)
{
if (left[i] <= right[j])
{
lr[k++] = left[i++];
}
else
{
lr[k++] = right[j++];
c += L-i;
}
}
while (i < L)
{
lr[k++] = left[i++];
}
while (j < R)
{
lr[k++] = right[j++];
}
return lr;
} int biGlobalInverse(int *&A, const int n)
{
if (n <= 1) return 0; int mid = (n-1) >> 1;//记得n-1
int *left = new int[n];
int *right = new int[n];
int L = 0, R = 0; for ( ; L <= mid; L++)
{
left[L] = A[L];
}
for (int i = mid+1; i < n; i++, R++)
{
right[R] = A[i];
}
delete [] A; int c1 = biGlobalInverse(left, L);
int c2 = biGlobalInverse(right, R); int c = 0;
A = mergeArr(left, L, right, R, c); delete left;
delete right; return c1+c2+c;
} public:
void run()
{
int T = 0, N = 0;
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);
int *A = new int[N]; for (int i = 0; i < N; i++)
{
scanf("%d", &A[i]);
}
int loc = localInverse(A, N);
int glo = biGlobalInverse(A, N);
if (loc == glo) puts("YES");
else puts("NO");
}
}
}; int littleElephantAndPermutations()
{
LittleElephantAndPermutations le;
le.run();
return 0;
}

最新文章

  1. tomcat安装后,本地可以访问,远程不能访问
  2. mysql 连接空闲超8小时自动断开连接问题(linux)
  3. CSS书写规范及顺序
  4. POJ 2484 A Funny Game(博弈论)
  5. void与void *
  6. javaMail编写案列
  7. POJ 2771 二分图(最大独立集)
  8. 使用原生js写ajax
  9. Silverlight IIs发布问题
  10. java线程图
  11. HDU 1247 Hat’s Words(map,STL,字符处理,string运用)
  12. linux的7种运行级别
  13. 查看Oracle最耗时的SQL
  14. openStack centos6.4
  15. CentOS7快速配置nginx node mysql8.0
  16. OpenCV 闭合轮廓检测
  17. html网页中如何给文字加入下划线
  18. virtualenv+pyenv管理python工作环境
  19. 安全运维中基线检查的自动化之ansible工具巧用
  20. SAP从入门到精通 知识体系

热门文章

  1. SAP ABAP exporting list to memory ...SUBMIT 程序传输屏幕参数
  2. &quot;Invalid username/password or database/scan listener not up&quot;
  3. 图解UML类与类之间的六中关系
  4. HTTP POST请求的Apache Rewrite规则设置
  5. Missile:双状态DP
  6. 一个用 C 语言写的迷你版 2048 游戏,仅仅有 500个字符
  7. JavaScript面向对象编程(10)高速构建继承关系之对象拷贝
  8. Oracle控制文件操作
  9. 百度地图之UI控制
  10. 浅谈 PHP 变量可用字符