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

求大牛帮忙看一下,wa了无数次了

Posted by hongkevin at 2008-05-29 11:11:47 on Problem 2299
#include <stdio.h>
#include <stdlib.h>
__int64 count = 0;
void merge(__int64 *A,long p,long mid,long q)
{
	__int64* t1 = (__int64*)malloc(sizeof(__int64)*(mid-p+1));
	__int64* t2 = (__int64*)malloc(sizeof(__int64)*(q-mid));
	long i,j,k;
	for(i = 0;i<mid-p+1;i++)
		t1[i] = A[p+i];
	for(i = 0;i<q-mid;i++)
		t2[i] = A[mid+1+i];
	i=0;j=0;
	for(k=p;k<=q;k++)
	{	
		if(i<mid-p+1 && j<q-mid)
		{
			if(t1[i]<t2[j]  )
			{
				A[k] = t1[i++];
			}
			else
			{
				A[k] = t2[j++];
				count++;
			}
			continue;
		}
		if(i<mid-p+1)
		{
			count += (mid-p-i)*j ;
			for(;k<=q;k++)
				A[k] = t1[i++];
			break;;
		}
		if(j<q-mid)
		{	
			A[k] = t2[j++];
			continue;
		}

	}
	free(t1);
	free(t2);
}
void mergesort(__int64* A,long p,long q)
{
	long mid;
	if(p<q)
	{
		mid = (q+p)/2;
		mergesort(A,p,mid);
		mergesort(A,mid+1,q);
		merge(A,p,mid,q);
	}		
}


int main()
{
	long n,i;
	while(1)
	{
		scanf("%ld",&n);
		if(n!=0)
		{
			__int64* A = (__int64*)malloc(sizeof(__int64)*n);
			i = 0;
			while(i<n)
			{
				scanf("%ld",&A[i++]);
			}
			count = 0;
			mergesort(A,0,n-1);
			printf("%I64d\n",count);
			free(A);
		}
		else
			break;
	}
	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