hdu1394 Minimum Inversion Number(最小逆序数)

Posted brucemengbm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu1394 Minimum Inversion Number(最小逆序数)相关的知识,希望对你有一定的参考价值。

Minimum Inversion Number

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

Author

CHEN, Gaoli

Source

ZOJ Monthly, January 2003

先百度逆序数:在一个排列中。假设一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。逆序数为偶数的排列称为偶排列;逆序数为奇数的排列称为奇排列

如2431中,21。43,41,31是逆序,逆序数是4,为偶排列。

对于这个题求把第一个数放到最后一个数的最小逆序数,对于原序列而言,假设把第一个数放到最后一个数,逆序列添加n-num[0]+1,逆序列降低

num[0].

所以这道题:

#include <stdio.h>
int main()
{
	int num[5005],n;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		scanf("%d",&num[i]);
		int sum=0,temp;
		for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++)
		if(num[i]>num[j]) sum++;
		temp=sum;
		for(int i=n-1;i>=0;i--)
		{
			temp-=n-1-num[i];
			temp+=num[i];
			if(temp<sum)
			sum=temp;
		}
		printf("%d\n",sum);
	}
	return 0;
}



以上是关于hdu1394 Minimum Inversion Number(最小逆序数)的主要内容,如果未能解决你的问题,请参考以下文章

hdu1394 [Minimum Inversion Number]

HDU 1394 Minimum Inversion Number

HDU1394 Minimum Inversion Number

[HDU1394]Minimum Inversion Number

hdu 1394 Minimum Inversion Number - 树状数组

[hdu1394]Minimum Inversion Number(树状数组)