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:这个题测试数据不重要吧 关键看你怎么找出一种快速求逆序数的方法

Posted by cshqzhang at 2005-12-05 00:08:11 on Problem 2299
In Reply To:这个题测试数据不重要吧 关键看你怎么找出一种快速求逆序数的方法 Posted by:zzningxp at 2005-12-04 23:20:47
// 排序应该是没问题的,不知错哪里了
#include<iostream>
using namespace std;

long count;
__int64 c[500000];

void Merge(__int64 a[], long min, long mid, long max)
{
	__int64 * b = new __int64 [max - min + 1];
	long j = min, k = mid + 1;
	long i = 0;
	for(i = 0; j <= mid && k <= max; i++)
	{
		if(a[j] > a[k])
		{
			b[i] = a[k++];
			count += mid - j + 1; //计算count
		}
		else
		{
			b[i] = a[j++];
		}
	}
	if(j <= mid)
	{
		for(; j <= mid; j++)
			b[i++] = a[j];
	}
	if(k <= max)
	{
		for(; k <= max; k++)
		{
			b[i++] = a[k];
		}
	}
	
	for(i = 0,j = min; i < max - min + 1; i++,j++)
	{
		a[j] = b[i];
	}

	delete [] b;
}

void MSort(__int64 a[], long min, long max)
{
	if(max == min)
	{
		return;
	}
	long mid = (max + min)/2;
	MSort(a, min, mid);
	MSort(a, mid + 1, max);
	Merge(a, min, mid, max);
}
int main()
{
	long num;
	long i;
	scanf("%ld", &num);
	while(num > 0)
	{
		count = 0;
		for(i = 0; i < num; i++)
		{
			scanf("%I64d", &c[i]);
		}
		MSort(c, 0, i-1);
		//for(i = 0; i < num; i++)
	//		printf("%I64d  ", c[i]);
	//	printf("\n");
		printf("%ld\n", count);
		scanf("%ld", &num);
	}
	
	return 0;
}


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