[Tyvj1464]最远点对

【题目分析】:先凸包然后旋转卡壳。Master_Chivu说其实面积可以用叉积计算……然后我听了恍然大悟……因为我一直拿海伦公式算……

传送门1:http://cgm.cs.mcgill.ca/~orm/rotcal.frame.html

传送门2:http://blog.csdn.net/kaytowin/archive/2010/01/06/5140111.aspx

传送门3:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html

【Code】:

const
	eps=1e-12;
type
	point=record
		x,y:int64;
	end;
var
	p,q:array[0..200000]of point;
	n,m,i,j,k:longint; ans:int64;
function max(x,y:int64):int64;
	begin
		if x>y then exit(x); exit(y);
	end;
function dist2(p0,p1:point):int64;
	begin
		exit(sqr(p0.x-p1.x)+sqr(p0.y-p1.y));
	end;
procedure qsort(l,r:longint);
	var i,j:longint; t:point; x,y:extended;
	begin
		i:=l; j:=r; x:=p[(l+r)>>1].x; y:=p[(l+r)>>1].y;
		repeat
			while (p[i].x<x) or (p[i].x=x) and (p[i].y<y) do inc(i);
			while (p[j].x>x) or (p[j].x=x) and (p[j].y>y) do dec(j);
			if i<=j then begin
				t:=p[i]; p[i]:=p[j]; p[j]:=t; inc(i); dec(j);
			end;
		until i>j;
		if l<j then qsort(l,j); if i<r then qsort(i,r);
	end;
function cross(p0,p1,p2:point):extended;
	begin
		exit((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
	end;
begin
	readln(n);
	for i:=1 to n do readln(p[i].x,p[i].y);
	qsort(1,n);
	m:=0;
	for i:=1 to n do begin
		while (m>1) and (cross(q[m-1],q[m],p[i])<=eps) do dec(m);
		inc(m); q[m]:=p[i];
	end;
	k:=m;
	for i:=n-1 downto 1 do begin
		while (m>k) and (cross(q[m-1],q[m],p[i])<=eps) do dec(m);
		inc(m); q[m]:=p[i];
	end;
	if n>1 then dec(m);
	j:=2; ans:=0;
	for i:=1 to m do begin
		while (cross(q[i],q[i+1],q[j+1])>cross(q[i],q[i+1],q[j])) do j:=j mod m+1;
		ans:=max(ans,max(dist2(q[i],q[j]),dist2(q[i+1],q[j+1])));
	end;
	writeln(ans);
end.

posted on 2011-03-16 15:57  Skywalker_Q  阅读(390)  评论(0)    收藏  举报

导航