codechef Little Elephant and Permutations题解
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 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 1, 2, ..., 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题解的更多相关文章
- codechef Little Elephant and Bombs题解
The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...
- CodeChef November Challenge 2013 部分题解
http://www.codechef.com/NOV13 还在比...我先放一部分题解吧... Uncle Johny 排序一遍 struct node{ int val; int pos; }a[ ...
- CodeChef Little Elephant and Movies [DP 排列]
https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...
- CodeChef Little Elephant and Mouses [DP]
https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...
- codechef February Challenge 2018 简要题解
比赛链接:https://www.codechef.com/FEB18,题面和提交记录是公开的,这里就不再贴了 Chef And His Characters 模拟题 Chef And The Pat ...
- codechef Row and Column Operations 题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- codechef January Lunchtime 2017简要题解
题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...
- codechef January Challenge 2017 简要题解
https://www.codechef.com/JAN17 Cats and Dogs 签到题 #include<cstdio> int min(int a,int b){return ...
- codechef Sums in a Triangle题解
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear ...
随机推荐
- Android使用ksoap2-android调用WebService学习
之前主要做客户端UI交互,很少处理数据和接触服务端,但现在的移动设备根本不可能离得开网络连接,数据的交换.最近学习的是在android端如何去调用远程WebService,都说WebService是一 ...
- VC/MFC 在ListCtl 控件中随鼠标移动提示单元格信息
BEGIN_MESSAGE_MAP(CTipListCtrl, CListCtrl) //{{AFX_MSG_MAP(CTipListCtrl) ON_WM_MOUSEMOVE() ON_WM_DES ...
- Android编程心得-Handler与子线程的交互初步
在编写项目的时候,本人发现一个关于线程与Handler很容易犯的错误. 我有两个Activity,一个Activity在后台创建了一个线程并且启动,这个线程对象对应的实体实在另外一个Activity的 ...
- QQ圈子降级为“应用”后应关注其隐私设置
在之前的QQ版本中,QQ圈子的权限设置在“系统设置”对话框的“权限设置”中,如图所示. 但是在更新后的2013SP1版本中,“系统设置”对话框中的“权限设置”已经没有了“圈子权限” QQ圈子成了应用管 ...
- JavaScript—DOM操作
- 【iOS发展-44】通过案例谈iOS重构:合并、格式化输出、宏观变量、使用数组来存储数据字典,而且使用plist最终的知识
我们今天的情况下是第一个例子,下面的5一来通过切换页上一页下一页: (1)第一步,基本是以非常傻非常直接的方式来创建.这里用到的主要点有: --把对象变量设置为全局变量使得能够在其它方法中调用来设置它 ...
- [Cocos2d-x]代码段记录
一些零碎的代码,便于以后查找 1.添加动画 //添加动画帧 CCAnimation* animation = CCAnimation::create(); ; i< ;i++) { ] = {} ...
- OCA读书笔记(11) - 实现Oracle数据库审计
11 Implementing Oracle Database Auditing 描述DBA对于安全和审计的职责使能标准的数据库审计安全审计选项查看审计信息维护审计路径 最小权限原则只在计算机上安装所 ...
- 《Python学习手册》读书笔记
之前为了编写一个svm分词的程序而简单学了下Python,觉得Python很好用,想深入并系统学习一下,了解一些机制,因此开始阅读<Python学习手册(第三版)>.如果只是想快速入门,我 ...
- 肯德基champs各个字母代表什么_百度知道
肯德基champs各个字母代表什么_百度知道 肯德基champs各个字母代表什么