Hdu 4717 The Moving Points (three-way)

Source: Internet
Author: User

Give the two-dimensional coordinates of n starting points and the size and direction of the velocity. Ask at which time the maximum distance between all two points is the smallest.

// Time 78 ms; Memory 1316K  

 

#include<iostream>  #include<cstdio>  #include<cmath>  #define maxn 50000  #define maxm 305  #define sqr(x) ((x)*(x))  #define eps 1e-5    using namespace std;    double a[maxn],b[maxn],c[maxn];  int cas,cnt;    int sig(double x)  {      return (x>eps)-(x<-eps);  }    typedef struct point  {      double x,y;      point(double xx=0,double yy=0):x(xx),y(yy){}  }vector;    point pt[maxm];  vector vt[maxm];    vector operator - (point p,point q)  {      return vector(p.x-q.x,p.y-q.y);  }  double dot(vector p,vector q)  {      return p.x*q.x+p.y*q.y;  }  double cal(double t)  {      double tmp,res=a[0]*t*t+b[0]*t+c[0];      for(int i=1;i<cnt;i++)      {          tmp=a[i]*t*t+b[i]*t+c[i];          if(res<tmp) res=tmp;      }      return res;  }    int main()  {      int i,j,t,n;      double l,r,m1,m2,tmp;      vector v,w;      scanf("%d",&t);      while(t--)      {          scanf("%d",&n);          cnt=0;          l=r=0.0;          for(i=0;i<n;i++)              scanf("%lf%lf%lf%lf",&pt[i].x,&pt[i].y,&vt[i].x,&vt[i].y);          for(i=0;i<n;i++) for(j=i+1;j<n;j++)          {              v=vt[j]-vt[i],              w=pt[j]-pt[i],              a[cnt]=dot(v,v),              b[cnt]=2*dot(v,w),              c[cnt]=dot(w,w);              tmp=-b[cnt]/(2*a[cnt]);              if(r<tmp) r=tmp;              cnt++;          }          while(sig(r-l)>0)          {               m1=l+(r-l)/3;m2=r-(r-l)/3;              if(cal(m1)<cal(m2)) r=m2;              else l=m1;          }          printf("Case #%d: %.2lf %.2lf\n",++cas,l,sqrt(cal(l)));      }      return 0;  }  

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.