题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3694
思路:三分竟然能水过。。。。感觉不是很对。。。。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const double eps=1e-7; struct Node { double x,y; }; Node a[5]; double minx,maxx,miny,maxy; double dist(double x,double y) { double sum=0; for(int i=0;i<4;i++) sum+=sqrt((a[i].x-x)*(a[i].x-x)+(a[i].y-y)*(a[i].y-y)); return sum; } double checky(double x) { double ly,ry,midy,midmidy; ly=miny,ry=maxy,midy=(ly+ry)/2.0; while(ry-ly>eps) { midy=(ly+ry)/2.0; midmidy=(midy+ry)/2.0; if(dist(x,midy)<dist(x,midmidy)) ry=midmidy; else ly=midy; } return dist(x,midy); } int main() { while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a[0].x,&a[0].y,&a[1].x,&a[1].y,&a[2].x,&a[2].y,&a[3].x,&a[3].y)==8) { if(a[0].x<0) break; minx=a[0].x,maxx=a[0].x; miny=a[0].y,maxy=a[0].y; for(int i=0;i<4;i++) { minx=min(minx,a[i].x); maxx=max(maxx,a[i].x); miny=min(miny,a[i].y); maxy=max(maxy,a[i].y); } double lx,rx,midx,midmidx; lx=minx,rx=maxx,midx=(lx+rx)/2.0; while(rx-lx>eps) { midx=(lx+rx)/2.0; midmidx=(midx+rx)/2.0; if(checky(midx)<checky(midmidx)) rx=midmidx; else lx=midx; // cout<<midx<<endl; } //cout<<midx<<endl; printf("%.4f\n",checky(midx)); } return 0; }
时间: 2024-10-25 16:04:16