codechef Little Elephant and Permutations题解

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题解

时间: 2024-12-29 06:44:57

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 buildings placed in a row and numbered from left to right strating from 0. Each building i (except the first and the last) has exactly two adjacent build

Codechef July Challenge 2014部分题解

Dish Owner(并查集) 链接:http://www.codechef.com/JULY14/problems/DISHOWN/ 题意分析:本题主要操作就是给出0 x y时,拥有第x道菜的厨师与拥有第y道菜的厨师pk,谁拥有的所有菜的其中一道菜(不一定是x或y)的分值比较高谁就获胜,并赢得loser的所有菜.即比较的是每个人分值最高的菜,所以对于非loser的人来说,他的分值最高的菜是不变的.综合题意用并查集易解. #include <cstdio> const int Maxn=100

codechef Row and Column Operations 题解

You are given an N × N grid initially filled by zeros. Let the rows and columns of the grid be numbered from1 to N, inclusive. There are two types of operations can be applied to the grid: RowAdd R X: all numbers in the row R should be increased by X

codechef Correctness of Knight Move题解

Chef develops his own computer program for playing chess. He is at the very beginning. At first he needs to write the module that will receive moves written by the players and analyze it. The module will receive a string and it should report at first

codechef Sums in a Triangle题解

Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a program which will compute the largest of the sums of numbers that appear on the paths st

codechef Holes in the text 题解

Chef wrote some text on a piece of paper and now he wants to know how many holes are in the text. What is a hole? If you think of the paper as the plane and a letter as a curve on the plane, then each letter divides the plane into regions. For exampl

codeforces A. Slightly Decreasing Permutations 题解

Permutation p is an ordered set of integers p1,??p2,??...,??pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation

codechef Johnny and the Beanstalk 题解

One evening Johnny found some funny looking beens in his grandfather's garden shed, and decided to plant one of them. Next morning, to his surprise he found an enormous beanstalk growing in his back yard. Undaunted by its size, he decided to count it

CodeChef Little Elephant and Mouses [DP]

https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中有老鼠,如果大象所在的格子和某个有老鼠的格子的曼哈顿距离$\e$1,大象就会被那只老鼠吓到.求一条移动路径,使得吓到过大象的老鼠数量最少.n;m $\le$100. 开始刷济南集训hzc的课件啦! 最简单的一道$DP$ 一只老鼠只会吓大象一次$f[i][j][0/1]$记录从左还是上走来的,转移时讨