HDU 4717 The Moving Points

来源:互联网 发布:lolypop女装淘宝网 编辑:程序博客网 时间:2024/06/03 16:41


题意:给定n个点,每个点有自己的移动速度和方向,求出某一个时间内的最大距离的最小值~~~~


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717


思路:

刚开始以为时间是整数,所以没理解为什么在最后的输出时浮点数的格式,

果断改掉,时间不一定是整数,从而三分解决~~~

第一次使用三分,果断不熟,还好,,过掉


#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int Max=310;struct Point{double x,y;double vx,vy;};Point p[Max],a[Max];int n;double Distance(Point a,Point b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double F(double mid){    for(int j=0;j<n;j++){        p[j].x=a[j].x+p[j].vx*mid;        p[j].y=a[j].y+p[j].vy*mid;    }    double ans = -100000000,temp;    for(int k=0;k<n;k++){        for(int h=k+1;h<n;h++){            temp=Distance(p[k],p[h]);            if(ans < temp) ans = temp;        }    }    return ans;}int main(){    int T;    scanf("%d",&T);    for(int ncase = 1; ncase <= T; ncase++){        scanf("%d",&n);        for(int i=0;i<n;i++){            scanf("%lf %lf %lf %lf",&p[i].x,&p[i].y,&p[i].vx,&p[i].vy);            a[i].x=p[i].x;a[i].y=p[i].y,a[i].vx=p[i].vx,a[i].vy=p[i].vy;        }        double L = 0.0,R = 1000.0;        for(int i=0;i<100;i++){            double m1=L+(R-L)/3;            double m2=R-(R-L)/3;            if(F(m1)<F(m2)) R=m2;            else L = m1;        }        printf("Case #%d: %.2lf %.2lf\n",ncase,L,F(L));    }}



原创粉丝点击