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

我也用了两次sort,为啥TLE了?郁闷,谁帮我看下代码

Posted by qq312873977 at 2009-05-08 22:58:23 on Problem 1974
In Reply To:hehe,两次快排不会超时!爽inging! Posted by:hujk2008 at 2004-11-05 12:43:34
我也用了两次sort,为啥TLE了?郁闷,谁帮我看下代码

#include<iostream>
#include <algorithm>
using namespace std;
#define M 131075
struct stone
{
	int x,y;
};
stone stones[M];
bool cmp(stone a,stone b)
{
	if(a.x<b.x)
		return true;
	else if(a.x==b.x&&a.y<b.y)
		return true;
	else return false;

}
bool cmp2(stone a,stone b)
{
	if(a.y<b.y)
		return true;
	else if(a.y==b.y&&a.x<b.x)
		return true;
	else return false;
	
}
int main()
{
	freopen("in.txt","r",stdin);
	int t,m,n,k,i,j,r,c,pos,tmp;
	
	cin>>t;
	while(t--)
	{
		cin>>m>>n>>k;
		pos=0;
		for(i=0;i<k;i++)
		{
			cin>>r>>c;
			stones[i].x=r-1;
			stones[i].y=c-1;
		}
		sort(stones,stones+k,cmp);
		pos+=stones[0].x;
		if(stones[0].y>=2)
			pos++;
		for(i=0;i<k-1;i++)
		{
			if(stones[i+1].x==stones[i].x&&stones[i+1].y-stones[i].y>=3)
				pos++;
			else if(stones[i+1].x>stones[i].x)
			{
				pos+=stones[i+1].x-stones[i].x-1;
				if(stones[i+1].y>=2)
					pos++;
				if(n-1-stones[i].y>=2)
					pos++;
			}
		}
		pos+=m-stones[k-1].x-1;
		if(n-1-stones[k-1].y>=2)
			pos++;

		sort(stones,stones+k,cmp2);
		pos+=stones[0].y;
		if(stones[0].x>=2)
			pos++;
		for(i=0;i<k-1;i++)
		{
			if(stones[i+1].y==stones[i].y&&stones[i+1].x-stones[i].x>=3)
				pos++;
			else if(stones[i+1].y>stones[i].y)
			{
				pos+=stones[i+1].y-stones[i].y-1;
				if(stones[i+1].x>=2)
					pos++;
				if(m-1-stones[i].x>=2)
					pos++;
			}
		}
		pos+=n-stones[k-1].y-1;
		if(m-1-stones[k-1].x>=2)
			pos++;
		cout<<pos<<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