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

我的快排过不了啊,总是TLE,求优化。(附代码)(虽然用STL过了)

Posted by 00946188 at 2010-10-08 18:33:30 on Problem 1002
void qs(int s[],int l,int r)
{
	int p,i,j;
	int t,temp;
	if(l>=r)
		return;
	if(r-l<=16)
	{
		for(i=l;i<r;i++)
			for(j=i+1;j<=r;j++)
				if(s[i]>s[j])
				{
					t=s[i];
					s[i]=s[j];
					s[j]=t;
				}
		return;
	}
	p=rand()%(r-l)+l;
	temp=s[p];
	s[p]=s[r];
	for(j=r,i=-1;i<j;)
	{
		for(;i<j;)
			if(s[++i]>temp)
			{
				s[j]=s[i];
				break;
			}
		for(;i<j;)
			if(s[--j]<temp)
			{
				s[i]=s[j];
				break;
			}
	}
	s[i]=temp;
	qs(s,l,i-1);
	qs(s,i+1,r);
}

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