hdu 1394 Minimum Inversion Number 归并求逆序数

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 12107    Accepted Submission(s): 7388

Problem Description

The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:

a1, a2, ..., an-1, an (where m = 0 - the initial seqence)

a2, a3, ..., an, a1 (where m = 1)

a3, a4, ..., an, a1, a2 (where m = 2)

...

an, a1, a2, ..., an-1 (where m = n-1)

You are asked to write a program to find the minimum inversion number out of the above sequences.

Input

The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.

Output

For each case, output the minimum inversion number on a single line.

Sample Input

10
1 3 6 9 0 8 5 7 4 2

Sample Output

16

昨天做了几道逆序数的题,全用的树状数组解决的。晚上睡觉的时候发现,树状数组求逆序数,还是有很多麻烦的问题的,1,当序列的的离散程度比较大的大时候,,直接用树状数组,,会MLE,需要离散化序列,虽然不是太难,可模块一多,难免会出现一些比较隐晦的bug,在比赛的时候最忌讳这个了。	2,当序列中有负数的时候,,可能我们就得重新选定坐标原点了,这个原点的坐标得根据数据的范围来选,如果题目说明的不清楚的时候,,就很坑了。

所以我决定再复习一下归并求逆序数,毕竟这个没什么限制,虽然速度不太怎么满意,但好在实用性比较强。

这道题就是给定一个序列,a1, a2, ..., an-1, an (where m = 0 - the initial seqence)

让你把按照这些顺序的序列的逆序数全求出来:

a2, a3, ..., an, a1 (where m = 1)
	a3, a4, ..., an, a1, a2 (where m = 2)
	...
	an, a1, a2, ..., an-1 (where m = n-1)
然后把最小的输出来。

没什么好说的,直接模板,

下面是代码:
#include <stdio.h>
#define MAX 10000
#define INF 100000000
int b[MAX];

int merge(int a[] , int start , int mid , int end)
{
	int i = start , j = mid+1 , k = start , count = 0;
	while(i<=mid && j<=end)
	{
		if(a[i]<=a[j])
		{
			b[k++] = a[i++];
		}
		else
		{
			b[k++] = a[j++];
			count += mid-i+1;
		}
	}
	while(i<=mid)
	{
		b[k++] = a[i++] ;
	}
	while(j<=end)
	{
		b[k++] = a[j++] ;
	}
	for(i = start ; i <= end ; ++i)
	{
		a[i] = b[i] ;
	}
	return count ;
}

int mergeSort(int a[],int start , int end)
{
	int sum = 0 ;
	if(start == end)
	{
		return 0;
	}
	int mid = (start+end)>>1 ;
	sum +=mergeSort(a,start,mid) ;
	sum +=mergeSort(a,mid+1,end) ;
	sum +=merge(a,start,mid,end) ;
	return sum ;
}

int min(int a , int b)
{
	return a>b?b:a ;
}
int main()
{
	int n ;
	while(~scanf("%d",&n))
	{
		int a[MAX],temp[MAX];
		for(int i = 0 ; i < n ; ++i)
		{
			scanf("%d",&a[i]);
			temp[i] = a[i] ;
		}
		int sum = INF ,ans;
		sum = mergeSort(a,0,n-1) ;
		ans = sum ;
		for(int i = 0 ; i < n-1 ; ++i)
		{
			sum += -temp[i]+ n-temp[i]-1;
			ans = min(sum,ans) ;
		}

		printf("%d\n",ans) ;
	}

	return 0;
}

AC状态:

时间: 2024-08-28 09:09:00

hdu 1394 Minimum Inversion Number 归并求逆序数的相关文章

HDU 1394——Minimum Inversion Number(最小逆序数)

题意: 给定一个序列,里面的数是0到n-1,  每次要把第一个数放到最后一个数,重复n次,求n次操作中最小的逆序数是多少? 思路: 先求出初始的逆序数,然后每移动第一个数到最后面,那么逆序数要减去比它小的数的个数,加上比它大的数的个数. 如果我输入的数是a[i],那么比它小的数的个数就有a[i]个,比它大的数的个数就有n-1-a[i]个 方法:    归并排序 ,树状数组,线段树 归并排序代码: #include<iostream> #include<cstring> #inclu

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

HDU 1394 Minimum Inversion Number(线段树求逆序数)

题目地址:HDU 1394 这题可以用线段树来求逆序数. 这题的维护信息为每个数是否已经出现.每次输入后,都从该点的值到n-1进行查询,每次发现出现了一个数,由于是从该数的后面开始找的,这个数肯定是比该数大的.那就是一对逆序数,然后逆序数+1.最后求完所有的逆序数之后,剩下的就可以递推出来了.因为假如目前的第一个数是x,那当把他放到最后面的时候,少的逆序数是本来后面比他小的数的个数.多的逆序数就是放到后面后前面比他大的数的个数.因为所有数都是从0到n-1.所以比他小的数就是x,比他大的数就是n-

HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由[1,N]构成,可以通过旋转把第一个移动到最后一个. 问旋转后最小的逆序数对. 分析: 注意,序列是由[1,N]构成的,我们模拟下旋转,总的逆序数对会有规律的变化. 求出初始的逆序数对再循环一遍就行了. 至于求逆序数对,我以前用归并排序解过这道题:点这里. 不过由于数据范围是5000,所以完全可以用线

HDU 1394 Minimum Inversion Number(线段树求逆序对)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1394 解题报告:给出一个序列,求出这个序列的逆序数,然后依次将第一个数移动到最后一位,求在这个过程中,逆序数最小的序列的逆序数是多少? 这题有一个好处是输入的序列保证是0 到 n-1,所以不许要离散化,还有一个好处就是在计算在这个序列中比每个数大和小的数一共有多少个的时候可以在O(1)时间计算出来,一开始我没有意识到,还傻傻的用了两层for循环来每次都计算,当然这样果断TLE了.把一个数从第一个移

HDU - 1394 Minimum Inversion Number (线段树求逆序数)

Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seq

HDU 1394 Minimum Inversion Number(逆序数)

题目链接:HDU 1394 Minimum Inversion Number [题意]给你一个1~N的数字组成的初始序列,然后每一次都将第一个数字移到最后,形成新的序列,然后求出这些序列的逆序数中的最小值. [思路]开始可以用任意一种方法(线段树 or 暴力 or 树状数组)计算出初始数列的逆序数sum,这里我比较懒,就直接用的暴力找的sum,对于a[i](0~~n-1),每挪一个,用sum减去挪之前它右边比它小的数的个数(也就是a[i]个),再用sum加上挪之后左边比它大的数的个数(也就是n-

hdu 1394 Minimum Inversion Number

题目链接:hdu 1394 Minimum Inversion Number 该题是求最小逆序对的扩展.可以使用树状数组来实现.对于$n$个数的序列$A$,其第$i$个数($i\in [0,n)$)的逆序数$r_i$可以表示为它的角标$i$减去在它之前且不大于它的数的个数.例如对序列A = {1,3,5,9,0,8,5,7,4,2}中的数,A[8] = 4.其逆序数$r_8 = 8 - 3 = 5$,第二个3表示三个在它前面且比它小的数:{1,3,0}.从而我们可以得到第$i$个数的逆序数公式:

hdu 1394 Minimum Inversion Number(线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10853    Accepted Submission(s): 6676 Problem Description The inversion number of a given number sequence a1, a2, ..., a