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

哪位牛人帮我看下,急!!为什么用了归排还是超时

Posted by 0608060202 at 2008-03-16 20:28:39 on Problem 2299
#include <iostream>
using namespace std; 

long Unsort[500010] , done[500010] ;
long count ;


void Merge( long left , long mid , long right )
{
	long i = left , j = mid+1 , k = left ;
	while (  i <= mid &&  j <= right )
	{
		if ( Unsort[i] > Unsort[j]  )
		{  done[k++] = Unsort[j++] ; }
		else
		{
			done[k++] = Unsort[i++] ;
		}
	}
	if ( i > mid )
		for ( long q = j ; q <= right ; q ++  )
			done[k++] = Unsort[q] ;
	else
		for ( long q = i ; q <= mid ; q ++ )
			done[k++] = Unsort[q] ;
	for ( long m = mid+1 ;m <= right ; m ++ )
	for ( long  n = mid ; n >= left ; n -- )
			if ( Unsort[m] < Unsort[n] )
				count ++ ;
}

void Copy( long left , long right )
{
	for ( long i = left ; i <= right ; i ++ )
		Unsort[i] = done[i] ;
}

void Megersort( long left , long right )
{
	if ( left < right )
	{
		long i = (left+right)/2 ;
		Megersort( left , i ) ;
		Megersort(  i+1 , right ) ;
		Merge( left , i , right ) ;
		Copy( left , right ) ;
	}
}

int main()
{
	long n ;
	while ( cin >> n , n )
	{
		count = 0 ;
		for ( long i = 0 ; i < n ; i ++ )
			cin >> Unsort[i] ;
		Megersort( 0 , n-1 ) ;
		cout << count <<endl ;
	}
	
	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