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为什么老是这样啊

Posted by ACM1900 at 2010-08-10 16:32:19 on Problem 2187
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

#define Max 50001

struct Point
{	int x,y;};

int sig(int a)
{	if(!a)
		return 0;
	return a>0? 1:-1;
}

Point p[Max];int stack[Max];
int top,n;

int  dis(Point p1,Point p2)
{	int d=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
	return d;
}

int cross(Point p1,Point p2,Point p3)
{	return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);}

bool comp(Point p1,Point p2)
{	if(p1.y==p2.y)
		return p1.x<p2.x;
	else
		return p1.y<p2.y;
}

void graham()
{	if(n<=1)
	{	top=1;
		return ;
	}
	sort(p+1,p+n+1,comp);
	top=0;
	stack[++top]=1;
	stack[++top]=2;
	for(int i=3;i<=n;i++)
	{	if(top>=2&&sig(cross(p[stack[top-1]],p[i],p[stack[top]]))>=0)top--;
		stack[++top]=i;
	}
	int temptop=top;
	stack[++top]=n-1;
	for(int i=n-2;i>=1;i--)
	{	if(top>=temptop+1&&sig(cross(p[stack[top-1]],p[i],p[stack[top]]))>=0)top--;
		stack[++top]=i;
	}
	int max=-1;
   for(int i=1;i<=top;++i)
    {   for(int j=i;j<=top;++j)
        {  int tem=dis(p[stack[i]],p[stack[j]]);
          if( tem>max )
             max=tem;
        }
    }
   
   printf("%d\n",max);
return ;
}


int main()
{
	while(cin>>n)
	{	for(int i=1;i<=n;i++)
			cin>>p[i].x>>p[i].y;
		graham();
	}
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