hdu 4071& poj 3873 & zoj 3386 & uva 12197 Trick or Treat 三分法

思路:

看到这个题目就发现所需最短时间也就是房子和相遇点的最远距离具有凹凸性,很容易就想到了三分法枚举。

找出所有房子的X坐标的最小最大值作为上下界。

代码如下:

 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<algorithm>
 4 #define M 50005
 5 #include<iostream>
 6 #define inf 200005
 7 #define eps 1e-8
 8 using namespace std;
 9 struct node
10 {
11     double x,y;
12 }p[M];
13 int n,cnt;
14 double dis(node a,double b)
15 {
16     return sqrt((a.x-b)*(a.x-b)+a.y*a.y);
17 }
18 double cal(double a)
19 {
20     double ans=0;
21     for(int i=0;i<n;i++){
22         double t=dis(p[i],a);
23         if(ans<t) ans=t;
24     }
25     return ans;
26 }
27 int main()
28 {
29     double l,r,m,mm;
30     while(scanf("%d",&n)&&n){
31         l=inf;r=-inf;
32         for(int i=0;i<n;i++){
33             scanf("%lf %lf",&p[i].x,&p[i].y);
34             if(p[i].x<l) l=p[i].x;
35             if(p[i].x>r) r=p[i].x;
36         }
37         while(r-l>=eps){
38             m=(l+r)/2;
39             mm=(l+m)/2;
40             double t=cal(m);
41             double tt=cal(mm);
42             if(t>=tt) r=m;
43             else l=mm;
44         }
45         printf("%.9lf %.9lf
",r,cal(r));
46     }
47     return 0;
48 }
View Code
原文地址:https://www.cnblogs.com/xin-hua/p/3357708.html