[Tyvj1457]多边形统计

【题目分析】:很裸的凸包,数据还很弱……

【Code】:

type
	point=record
		x,y:extended;
	end;
var
	p,q:array[0..100]of point;
	n,m,i,k:longint; ans:extended;
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;
function dist(p0,p1:point):extended;
	begin
		exit(sqrt(sqr(p0.x-p1.x)+sqr(p0.y-p1.y)));
	end;
function area(p0,p1,p2:point):extended;
	var a,b,c,p:extended;
	begin
		a:=dist(p0,p1); b:=dist(p1,p2); c:=dist(p2,p0);
		p:=(a+b+c)/2;
		exit(sqrt(p*(p-a)*(p-b)*(p-c)));
	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])<=0) 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])<=0) do dec(m);
		inc(m); q[m]:=p[i];
	end;
	if n>1 then dec(m);
	if m=2 then begin
		writeln(dist(q[1],q[2]):0:2); writeln('0.00');
	end else begin
		ans:=dist(q[m],q[1]);
		for i:=1 to m-1 do ans:=ans+dist(q[i],q[i+1]);
		writeln(ans:0:2);
		ans:=0;
		for i:=2 to m-1 do ans:=ans+area(q[1],q[i],q[i+1]);
		writeln(ans:0:2);
	end;
end.

posted on 2011-03-16 14:21  Skywalker_Q  阅读(242)  评论(0编辑  收藏  举报

导航