首页 > 代码库 > HDU1394 Minimum Inversion Number 【线段树】+【逆序数】

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 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,为偶排列。

逆序数计算方法是:在逐个元素读取原始数列时,每次读取都要查询当前已读取元素中大于当前元素的个数,加到sum里,最后得到的sum即为原始数列的逆序数。

接下来找数列平移后得到的最小逆序数,假设当前序列逆序数是sum,那么将a[0]移到尾部后逆序数的改变是之前比a[0]大的数全部与尾部a[0]组合成逆序数,假设数量为x,则x=n-1-a[0],而之前比a[0]小的数(也就是之前能和a[0]组合为逆序数的元素)不再与a[0]组合成逆序数,假设数量为y,则y=n-x-1,这样,新序列的逆序数就是sum+x-y=sum-2*a[0]+n-1;

接下来说明下线段树的作用,线段区间表示当前已读取的元素个数,比如[m,n]表示在数字m到n之间有多少个数已经读入,build时所有树节点全部为0就是因为尚未读数,update函数是将新读入的数字更新到线段树里,点更新,query函数是查询当前数字区间已存在的数字个数。

思路捋清晰了,接下来就该coding了~~

#include <stdio.h>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define maxn 5002

int tree[maxn << 2];
int arr[maxn];

void build(int l, int r, int rt)
{
	tree[rt] = 0;
	if(l == r) return;
	
	int mid = (l + r) >> 1;
	build(lson);
	build(rson);
}

void update(int pos, int l, int r, int rt)
{
	if(l == r){
		tree[rt] = 1; return;
	}
	
	int mid = (l + r) >> 1;
	if(pos <= mid) update(pos, lson);
	else update(pos, rson);
	
	tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}

//询问区间[left, right]中目前的数字个数
int query(int left, int right, int l, int r, int rt)
{
	if(left == l && right == r) return tree[rt];
	
	int mid = (l + r) >> 1;
	if(right <= mid) return query(left, right, lson);
	else if(left > mid) return query(left, right, rson);
	
	return query(left, mid, lson) + query(mid + 1, right, rson);
}

int main()
{
	int n, i, ans, sum;
	while(scanf("%d", &n) == 1){
		build(0, n - 1, 1);
		sum = 0; //求原始数列的逆序数
		
		for(i = 0; i < n; ++i){
			scanf("%d", arr + i);
			//查询当前比arr[i]大的数有多少个
			sum += query(arr[i], n - 1, 0, n - 1, 1);
			update(arr[i], 0, n - 1, 1);
		}
		
		ans = sum;
		for(i = 0; i < n; ++i){
			sum = sum - 2 * arr[i] + n - 1;
			if(sum < ans) ans = sum;
		}
		
		printf("%d\n", ans);
	}
	return 0;
}