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. MFC实现多风格真彩色大图标工具栏按钮

    研究zlib库,想实现一个类似winrar功能的小东东,打开winrar界面看它的工具栏比较好看于是动手想做一个,当然资源也使用的是winrar附带的.下面是截图:真彩色(32位)32*32大图标工具 ...

  2. 几款屏幕录制软件 ActivePresente

    几款屏幕录制软件,最强大是  ActivePresenter ,免费版, 足以应对我们日常需求.列表如下 支持系统:W-Windows,L-Linux,M-Mac 软件 格式 W L M 免费 说明 ...

  3. OCA读书笔记(4) - 管理数据库实例

    Objectives: •Start and stop the Oracle database and components •Use Oracle Enterprise Manager •Acces ...

  4. DirectX SDK版本与Visual Studio版本

    对于刚刚接触 DirectShow 的人来说,安装配置是一个令人头疼的问题,经常出现的情况是最基本的 baseclass 就无法编译.一开始我也为此费了很大的功夫,比如说修改代码.修改编译选项使其编译 ...

  5. 多校第五场 归并排序+暴力矩阵乘+模拟+java大数&amp;记忆化递归

    HDU 4911 Inversion 考点:归并排序 思路:这题呀比赛的时候忘了知道能够用归并排序算出逆序数,可是忘了归并排序的实质了.然后不会做-- 由于看到题上说是相邻的两个数才干交换的时候.感觉 ...

  6. 【m从翻译os文章】写日志禁令Sqlnet.log和Listener.log

    写日志禁令Sqlnet.log和Listener.log 参考原始: How to Disable Logging to the Sqlnet.log and the Listener.log (Do ...

  7. 解决Andriod使用HttpURLConnection 失败问题

    在Android的Activity中使用HttpURLConnection连接到服务端时抛出异常,Access denied.第一个想到是权限问题.然后就尝试将INTERNET权限加上:在Manife ...

  8. [Cocos2d-x学习笔记]Android NDK: Host 'awk' tool is outdated. Please define NDK_HOST_AWK to point to Gawk or Nawk解决方案

    Android NDK: Host 'awk' tool is outdated. Please define NDK_HOST_AWK to point to Gawk or Nawkawk过期网上 ...

  9. 怎样用Google APIs和Google的应用系统进行集成(1)----Google APIs简介

    Google的应用系统提供了非常多的应用,比方 Google广告.Google 任务,Google 日历.Google blogger,Google Plus,Google 地图等等非常的多的应用,请 ...

  10. Python什么是二次开发的意义?python在.net项目采用

    任何人都知道python在.net该项目是做什么的啊? 辅助用途,用作"二次开发"..net站点的话python主要是CGI才用.能够用python编写B/S程序. 解释一下二次开 ...