算法请自行百度,本文专门用来贴代码,想学算法的请直接无视
tyvj的某题
Code:
type
int=longint;
real=extended;
point=record
x,y:real;
end;
line=record
a,b,c:real;
end;
const e=1e-10;maxn = 100010;
operator *(a,b:point)c:real;
begin c:=a.x*b.y-a.y*b.x;end;
operator -(a,b:point)c:point;
begin c.x:=a.x-b.x;c.y:=a.y-b.y;end;
function d(a,b:point):real;
begin d:=sqr(a.x-b.x)+sqr(a.y-b.y);end;
var
i,j,m,n:int;k,x:point;
max,ans,t:real;
a:array[1..maxn]of point;
p:array[0..maxn]of int;
procedure sort(l,r:int);
var i,j:int;m:real;
begin
m:=a[(l+r)>>1].x;
i:=l;j:=r;
repeat
while a[i].x<m do inc(i);
while a[j].x>m do dec(j);
if i<=j then begin
k:=a[i];a[i]:=a[j];a[j]:=k;
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end;
procedure hull();
begin
m:=1;p[0]:=1;p[1]:=2;
for i:=3 to n do begin
while(m<>0)and((a[i]-a[p[m]])*(a[p[m]]-a[p[m-1]])>0)do dec(m);
inc(m);p[m]:=i;
end;
inc(m);p[m]:=n-1;
for i:=n-2 downto 1 do begin
while(m<>1)and((a[i]-a[p[m]])*(a[p[m]]-a[p[m-1]])>0)do dec(m);
inc(m);p[m]:=i;
end;
end;
function deal(x,y,z:int;var max:real):boolean;
begin
t:=(a[p[x]]-a[p[z]])*(a[p[y]]-a[p[z]]);
if t<0 then t:=-t;
if max>t then exit(false);
max:=t;deal:=true;
end;
procedure main;
begin
j:=1;
for i:=0 to m-1 do begin
max:=(a[p[i]]-a[p[j]])*(a[p[i+1]]-a[p[j]]);
if max<0 then max:=-max;
while(deal(i,i+1,(j+1)mod(m+1),max))do j:=(j+1)mod(m+1);
t:=d(a[p[i]],a[p[j]]);
if t>ans then ans:=t;
t:=d(a[p[i]],a[p[i+1]]);
if t>ans then ans:=t;
end;
end;
begin
assign(input,'data.txt');reset(input);
assign(output,'ans.txt');rewrite(output);
read(n);
for i:=1 to n do begin
read(a[i].x,a[i].y);
a[i].x:=a[i].x+(random*e)/100;
a[i].y:=a[i].y+(random*e)/100;
end;
sort(1,n);
hull;
main;
write(ans:0:0);
close(input);close(output);
end.
半平面交+旋转卡壳,花了我一下午+一节晚自习TAT
Code:
program circles;
uses math;
type
int=longint;
real=extended;
point=record
x,y:real;
end;
line=record
a,b,c:real;
end;
const
maxn=70000;
e=1e-7;
operator +(a,b:point)c:point;
begin c.x:=a.x+b.x;c.y:=a.y+b.y;end;
operator -(a,b:point)c:point;
begin c.x:=a.x-b.x;c.y:=a.y-b.y;end;
operator *(a,b:point)c:real;
begin c:=a.x*b.y-a.y*b.x;end;
operator **(a,b:point)c:real;
begin c:=a.x*b.x+a.y*b.y;end;
operator /(a,b:point)c:real;
begin c:=sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));end;
operator *(a,b:line)c:point;
var s,x,y:real;
begin
s:=b.b*a.a-a.b*b.a;
y:=b.c*a.a-a.c*b.a;
x:=b.c*a.b-a.c*b.b;
c.x:=-x/s;c.y:=y/s;
end;
operator =(a:point;b:line)c:boolean;
begin c:=b.a*a.x+b.b*a.y-b.c<-e;end;
var
i,j,m,n,h:int;
a,c:array[0..maxn]of point;
l:array[0..maxn]of line;
p:array[0..maxn]of int;
lb,rb,mid,z,ans:real;
t,k,source:point;
procedure up(var x:int);
begin x:=(x+1)mod(m+1);end;
function plane:boolean;
begin
m:=2;h:=2;
p[1]:=1;p[2]:=2;
c[2]:=l[1]*l[2];
for i:=3 to n do begin
while(h<=m)and(c[m]=l[i])do dec(m);
while(h<=m)and(c[h]=l[i])do inc(h);
if l[i].b*l[p[m]].a-l[p[m]].b*l[i].a=0 then exit(false);
inc(m);p[m]:=i;
c[m]:=l[i]*l[p[m-1]];
end;
while(h<m)and(c[m]=l[p[h-1]])do dec(m);
while(h<m)and(c[h]=l[p[m]])do inc(h);
if m-h<1 then exit(false);
c[m+1]:=l[p[m]]*l[p[h-1]];
exit(true);
end;
function maxi(var x:real;y:real):boolean;
begin
maxi:=x<y;
if x<y then x:=y;
end;
function ok(x:real):boolean;
begin
for i:=1 to n do begin
t.x:=l[i].a;t.y:=l[i].b;
l[i].c:=t**a[i]+x*(t/source);
end;
if not plane then exit(false);
for i:=0 to m-h+1 do c[i]:=c[i+h];
m:=m-h+1;c[m+1]:=c[0];
j:=1;ans:=-1;
for i:=0 to m-1 do begin
z:=(c[i]-c[j])*(c[i+1]-c[j]);up(j);
while(maxi(z,(c[i]-c[j])*(c[i+1]-c[j])))do up(j);
dec(j);if j<0 then j:=m;
z:=c[i]/c[j];
if z>ans then ans:=z;
z:=c[i+1]/c[i];
if z>ans then ans:=z;
end;
ok:=ans/2>x;
end;
begin
assign(input,'2circles.in');reset(input);
assign(output,'2circles.out');rewrite(output);
randomize;
read(n);
fillchar(source,sizeof(source),0);
for i:=1 to n do read(a[i].x,a[i].y);
a[n+1]:=a[1];
for i:=1 to n do begin
t:=a[i+1]-a[i];
l[i].a:=-t.y;l[i].b:=t.x;
end;
lb:=0;rb:=1e8;
while rb-lb>0.0003 do begin
mid:=(lb+rb)/2;
if ok(mid)then lb:=mid
else rb:=mid;
end;
write(lb:0:3);
close(input);close(output);
end.