Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:怎么能快些?我写的求逆序数的函数是 n^2 的,总复杂度是 mn^2, 请教简单方法

Posted by Afteryou at 2005-11-29 00:35:02 on Problem 1007
In Reply To:怎么能快些?我写的求逆序数的函数是 n^2 的,总复杂度是 mn^2, 请教简单方法 Posted by:sunmoonstar_love at 2005-08-07 16:03:10
通过使用修改后的归并排序算法,可以使算法复杂度降为nlgn,但是对于此题,最好不要用,我用的后果就是tle,在如此小的规模下,函数调用所花的时间占程序总执行时间的比例太大,得不偿失。
int Merge_Inversion(char * A,int p, int q, int r){
	int result;
	int n1, n2;
	char L[50];
	char R[50];
	int i, j, k;
	result = 0;
	n1 = q - p + 1;
	n2 = r - q;
	for(i = 0; i < n1; i++){
		L[i] = A[p + i];
	}
	for(j = 0; j < n2; j++){
		R[j] = A[q + j + 1];
	}
	i = 0;
	j = 0;
	k = p;
	while(i < n1 && j < n2){
		if((int)R[j] < (int)L[i]){
			A[k++] = R[j++];
			result = result + n1 - i;
		}
		else{
			A[k++] = L[i++];
		}
	}
	for(; i < n1; i++)
		A[k++] = L[i];
	for(; j < n2; j++)
		A[k++] = R[j];
	return result;
}
int Total_Inversion(char * A,int low,int high){
	int mid, a, b, c;
	if(low == high)
		return 0;
	mid = (low + high) / 2;
	a = Total_Inversion(A, low, mid);
	b = Total_Inversion(A, mid + 1, high);
	c = Merge_Inversion(A, low, mid, high);
	return a + b + c;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator