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



Statistic | Submit | Back

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

如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;
}
时间: 2024-08-06 11:50:28

hdu1394 Minimum Inversion Number(最小逆序数)的相关文章

Minimum Inversion Number_最小逆序数

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

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 【逆序数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目大意:给一个n,然后给出一组0~n-1的序列,求这个序列的逆序数,以及交换之后的逆序数中的最小值.交换规则是: 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

HDU1394 Minimum Inversion Number 【线段树】+【逆序数】

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

HDU1394 Minimum Inversion Number 线段树+数学

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

hdu1394 [Minimum Inversion Number]

Minimum Inversion Number 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 e

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

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

hdu1394 Minimum Inversion Number(线段树单点更新||暴力)

题目链接: huangjing 这个题目暴力和线段树都可以过,但是都需要掌握一个规律.. 当队首元素移到队尾后,可定会减少a[i]个逆序对,然后增加n-1-a[i]个逆序对. 你看比如1移到队尾,那么1>0这个逆序对就会减少,2>1,3>1,4>1这些逆序对就会增加.. 所以发现这个规律就好做了.. 暴力做法就是直接那样模拟.. 线段树做法是首先建立一颗空树,然后插入之前询问这颗树中在[插入元素,n]之间的树,求出来的就是逆序对的个数.然后将其更新进去即可.. 题目: 寻人启事:2

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): 16585 Accepted Submission(s): 10093 Problem Description The inversion number of a given number sequence a1, a2, -, an is th