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

megersort居然错?

Posted by 6233843 at 2006-10-22 21:45:24 on Problem 2299
#include <iostream>
using namespace std;

const int MAX = 500001;
long long a[MAX],b[MAX];
long long res;

void merge(int begin,int mid,int end)
{
 int i,j,k = 0;
 for(i = begin,j = mid+1; i <= mid && j <= end; k++){
  if(a[i] <= a[j])
   b[k] = a[i++];
  else {
   b[k] = a[j++];
   res += mid+1-i;
  }
 }
 for(; i <= mid; i++)
  b[k++] = a[i];
 for(; j <= end; j++)
  b[k++] = a[j];
 for(i = 0; i < k; i++)
  a[begin+i] = b[i];
}

void msort(int s, int t)
{
 if(s < t){
  int m = (s+t)>>1;
  msort(s,m);
  msort(m+1,t);
  merge(s,m,t);
 }
}

int main()
{
	int test;
	int i;
	while (scanf("%d",&test) && test)
	{
		res=0;
		for (i=0;i<test;i++)
		{
			scanf("%lld",&a[i]);
		}
		msort(0,test-1);
		printf("%lld\n",res);
	}
}
想不出来个理由?

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