TLE三个点
查看原帖
TLE三个点
305854
Drind楼主2020/5/3 17:51

如题,我的程序里有两种办法而已

#include<iostream>
using namespace std;
void qs(int *a,int left,int right)
{
	int t;
	if (left>=right)
		return;
	if (left==right-1)
	{
		if (a[left]>a[right])
			t=a[left],a[left]=a[right],a[right]=t;
		return;
	}
	int l,r,j,i;
	j=a[left];
	l=left;
	r=right;
	while(l<r)
	{
		while(a[r]>=j&&l<r)
			r--;
		while(a[l]<=j&&l<r)
			l++;
		//for(i=left;i<=right;i++)
		//	cout<<a[i]<<" ";
		//cout<<endl; 
		t=a[r];
		a[r]=a[l];
		a[l]=t;
	}
	a[left]=a[l];
	a[l]=j;
	qs(a,left,l-1);
	qs(a,l+1,right);
}

void sort(int *a,int left,int right)
{
	int t;
	if (left>=right)
		return;
	int l,r,j,i;
	j=a[(left+right)/2];
	l=left;
	r=right;
	while(l<r)
	{
		while(a[r]>j)
			r--;
		while(a[l]<j)
			l++;
		//for(i=left;i<=right;i++)
		//	cout<<a[i]<<" ";
		//cout<<endl; 
		t=a[r];
		a[r]=a[l];
		a[l]=t;
	}
	qs(a,left,l-1);
	qs(a,l+1,right);
}

int main()
{
	int i,n;
	int a[100001];
	cin>>n;
	for(i=1;i<=n;i++)
		cin>>a[i];
	sort(a,1,n);
	for(i=1;i<=n;i++)
		cout<<a[i]<<" ";
	return 0;
}

然而,两个办法都是TLE后三个点

2020/5/3 17:51
加载中...