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

初用graham 结果TLE了,哪位仁兄帮帮我看一下: 谢谢

Posted by zhb_msqx at 2007-08-30 22:36:10 on Problem 2187
#include <iostream>
#include <fstream>
using namespace std;

int pn;
struct point{
	int x,y;
	void print(){
		cout<<x<<" "<<y<<endl;
	}
} p[50010];

int stack[50010];
int sn;

int caldis(point p1,point p2){
	return (  (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)   );
}


int xmult(point p1,point p2,point p0){
	return  ( (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y)  );
}
int det(int x1,int y1,int x2,int y2){
	return (  x1*y2-x2*y1  );
}
int cmp(const void *a1,const void *a2){
	point s=*(point *)a1;point t=*(point *)a2;
	int sx=s.x-p[0].x;int sy=s.y-p[0].y;
	int tx=t.x-p[0].x;int ty=t.y-p[0].y;
	int res=det(sx,sy,tx,ty);
	if(res==0){
		return (  tx*tx+ty*ty-sx*sx-sy*sy   );
	}
	return res;
}


void init(){
	int min=0;
	int i,j,k;
	for(i=1;i<pn;i++){
		if(p[i].y<p[min].y||p[i].y==p[min].y&&p[i].x<p[min].x){
			min=i;
		}
	}
	std::swap(p[min],p[0]);
	qsort(&p[1],pn-1,sizeof(p[0]),cmp);
	int tmpn=2;
	for(i=2;i<pn;i++){        //除去极角相同的点
		if(xmult(p[i],p[i-1],p[0])!=0){
			p[tmpn++]=p[i];
		}
	}
	pn=tmpn;
/*	for(i=0;i<pn;i++)
		p[i].print();
	cout<<pn<<endl;
		*/
}
void convex(){
	int i,j,k;
	sn=0;
	stack[sn++]=0;
	stack[sn++]=1;
	stack[sn++]=2;
	
	int a=stack[sn-1];int b=stack[sn-2];
	for(i=3;i<pn;i++){
		while(xmult(p[i],p[a],p[b])<=0){
			sn--;
			a=stack[sn-1];b=stack[sn-2];
		}
		stack[sn++]=i;
	}
/*	for(i=0;i<sn;i++){
		p[stack[i]].print();
	}
*/
}



void main(){
//	ifstream cin("data.txt");
	cin>>pn;
	int i,j,k;
	for(i=0;i<pn;i++){
		cin>>p[i].x>>p[i].y;
	}
	init();
	convex();

	int maxdis=0;
	int mark[50010];
	memset(mark,0,sizeof(mark));
	for(i=0;i<pn;i++){
		for(j=0;j<pn;j++){
			if(i==j||mark[j]==1)continue;
			int curdis=caldis(p[i],p[j]);
			if(curdis>maxdis)maxdis=curdis;
		}
		mark[i]=1;
	}
	cout<<maxdis<<endl;
}

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