博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codechef Little Elephant and Permutations题解
阅读量:5924 次
发布时间:2019-06-19

本文共 3018 字,大约阅读时间需要 10 分钟。

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:41122 133 2 141 3 2 4Output:YESYESNOYES

总结一下有下面关系:

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

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

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

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

#pragma once#include 
#include
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;}

转载地址:http://eqxvx.baihongyu.com/

你可能感兴趣的文章
JAVA类的生命周期
查看>>
Linux服务器部署系列之七—OpenLDAP篇
查看>>
Python 在Ubuntu下的开发环境搭建
查看>>
沃通WoSign:关于微信公告“iOS11不再信赖WoSign证书”的说明
查看>>
CENTOS客户端加载ISCSI配置方法。
查看>>
11月全球操作系统版本份额混战:XP仅为13.57%
查看>>
10月8日“.我爱你”域名总量:耐思尼克升至十一名
查看>>
12月第3周网络安全报告:发现放马站点域名131个
查看>>
k8s集群之kubernetes-dashboard和kube-dns组件部署安装
查看>>
sed命令使用
查看>>
LAMP架构(Apache访问日志不记录静态文件、访问日志切割、静态元素过期时间)...
查看>>
设置trunk不行需要设置access才可以互通
查看>>
Zimbra 8.7.1GA更新
查看>>
how to install subversion(svn) with eclipse on windows
查看>>
linux下vi命令大全
查看>>
Node.js 应用故障排查手册 —— 利用 CPU 分析调优吞吐量
查看>>
链表笔试题汇编(二)
查看>>
团购网团挖员工陷混战
查看>>
MySQL登陆后提示符的修改
查看>>
Puppet之安装dashboard 成功版
查看>>