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;
}

codechef Little Elephant and Permutations题解的更多相关文章

  1. codechef Little Elephant and Bombs题解

    The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...

  2. CodeChef November Challenge 2013 部分题解

    http://www.codechef.com/NOV13 还在比...我先放一部分题解吧... Uncle Johny 排序一遍 struct node{ int val; int pos; }a[ ...

  3. CodeChef Little Elephant and Movies [DP 排列]

    https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...

  4. CodeChef Little Elephant and Mouses [DP]

    https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...

  5. codechef February Challenge 2018 简要题解

    比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...

  6. codechef Row and Column Operations 题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  7. codechef January Lunchtime 2017简要题解

    题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...

  8. codechef January Challenge 2017 简要题解

    https://www.codechef.com/JAN17 Cats and Dogs 签到题 #include<cstdio> int min(int a,int b){return ...

  9. codechef Sums in a Triangle题解

    Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...

随机推荐

  1. Ajax技术--考试计时并自动提交试卷

    1.概述 在开发网络考试系统时,考试计时并自动提交试卷是必不可少的功能.由于在答卷过程中,试卷不能刷新,所以需要使用Ajax实现无刷新操作.运行本实例,访问准备考试页面index.jsp,在该页面中, ...

  2. 自己定义android 4.0以上的对话框风格

    做个笔记.这里是Dialog的风格,假设是用AlertDialog创建的,不能直接用.在styles.xml的写法: <style name="DialogWindowTitle&qu ...

  3. Swift - 使用arc4random()、arc4random_uniform()取得随机数

    arc4random()这个全局函数会生成9位数的随机整数   1,下面是使用arc4random函数求一个1~100的随机数(包括1和100) 1 var temp:Int = Int(arc4ra ...

  4. Mybatis 3 返回布尔值,需要注意的地方

    在Mybatis中,有时候需要返回布尔值 ,来确定某个记录行是否存在. 例如: <select id="isExistCode" parameterType="st ...

  5. Selenium来抓取动态加载的页面

    一般的爬虫都是直接使用http协议,下载指定url的html内容,并对内容进行分析和抽取.在我写的爬虫框架webmagic里也使用了HttpClient来完成这样的任务. 但是有些页面是通过js以及a ...

  6. queue C++

    #include <iostream> using namespace std; class DequeEmptyException { public: DequeEmptyExcepti ...

  7. ORACLE数据库常见问题汇总

    提交事务的时候提示(数据库被一个用户锁住的解决方法) select object_id,session_id,locked_mode from v$locked_object; select t2.u ...

  8. 从零开始学C++之动态创建对象

    回顾前面的文章,实现了一个简单工厂模式来创建不同类对象,但由于c++没有类似new "Circle"之类的语法,导致CreateShape 函 数中需要不断地ifelse地去判断, ...

  9. hdu 2838 Cow Sorting(树状数组)

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  10. 初尝Java序列化/反序列化对象

    看个类: package com.wjy.bytes; import java.io.Serializable; public class ObjTest implements Serializabl ...