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

who can tell me why was it wa so many time?

Posted by fjnu_jxd_010 at 2005-10-04 18:17:11 on Problem 2299
#include<iostream>
using namespace std;
int sum,a[500017],b[500017];
int me(int low,int mid,int high)
{
	int t=mid,s=low,i,j;
        //if(mid-low+1>high-mid)
		//	n=mid-low+1;
	//	else n=high-mid;
		for(i=1;;i++)
		{
			if(mid+1>high||low>t)
				break;
			if(a[low]<=a[mid+1])
			{
				b[i]=a[low];
				low++;
			}
			else 
			{
				sum=sum+(t+1-low);
				b[i]=a[mid+1];
				mid++;
			}
		}  
		
		if(low<=t)
			for(j=i;;j++)
			{
				b[j]=a[low];
				low++;
				if(low>t)
					break;
			}
		else 
			if(mid+1<=high)
        
		for(j=i;;j++)
		 {
			b[j]=a[mid+1];
		        mid++;
		    if(mid+1>high)
			    break;
		 }

         for(i=1;i<=j;i++)
              a[i+s-1]=b[i];
        
return 0;
}
int mesort(int low,int high)
{
	int mid;
	if(low<high)
	{
		mid=(low+high)/2;
	    mesort(low,mid);
	    mesort(mid+1,high);
	    me(low,mid,high);
	}
	return 0;
}

int main()
{
	int n,i;
	while(scanf("%d",&n)==1&&n)
	{
		sum=0;
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);
         mesort(1,n);
		 printf("%d\n",sum);
	}
	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