码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1394 Minimum Inversion Number(最小逆序数)

时间:2015-08-13 14:28:07      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:hdu1394   hdu 1394   最小逆序数   

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   hdu 1394   最小逆序数   

原文地址:http://blog.csdn.net/su20145104009/article/details/47610993

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!