The Moving Points

本文介绍了一个算法问题,即在给定的点集合中,找到一个时间点使得任意两点间的最大距离达到最小,并计算该最小距离。通过三次搜索法解决,适用于多个点的动态位置跟踪场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
There are N points in total. Every point moves in certain direction and certain speed. We want to know at what time that the largest distance between any two points would be minimum. And also, we require you to calculate that minimum distance. We guarantee that no two points will move in exactly same speed and direction.
 

Input
The rst line has a number T (T <= 10) , indicating the number of test cases.
For each test case, first line has a single number N (N <= 300), which is the number of points.
For next N lines, each come with four integers Xi, Yi, VXi and VYi (-106 <= Xi, Yi <= 106, -102 <= VXi , VYi <= 102), (Xi, Yi) is the position of the ith point, and (VXi , VYi) is its speed with direction. That is to say, after 1 second, this point will move to (Xi + VXi , Yi + VYi).
 

Output
For test case X, output "Case #X: " first, then output two numbers, rounded to 0.01, as the answer of time and distance.
 

Sample Input
2 2 0 0 1 0 2 0 -1 0 2 0 0 1 0 2 1 -1 0
 

Sample Output
Case #1: 1.00 0.00 Case #2: 1.00 1.00
题意:给定n个点,以及每个点在某个方向的移动速度,求一个时刻是的n个点中任意两个点的距离最大的最小。
题解:直接对时间进行三分即可
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
struct lmx{
 double x;
 double y;
 double vx;
    double vy;
};
lmx lm[300];
double cnt(double x1,double y1,double x2,double y2)
{
 return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double maxi(double a,double b)
{
 return a>b?a:b;
}
int main()
{
     int test,i,ca=0,n,j;
  scanf("%d",&test);
  while(test--)
  {
   ca++;
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
            scanf("%lf %lf %lf %lf",&lm[i].x,&lm[i].y,&lm[i].vx,&lm[i].vy);
   }
   double l,r,mid,midmid,sx,sy,ex,ey;
   double mx,my,nx,ny;
   double ma=0,p1,p2;
   printf("Case #%d: ",ca);
                l=0,r=1e10;
    while(r-l>1e-6)
    {
     mid=(l+r)/2;
     midmid=(mid+r)/2;
     p1=0;p2=0;
     for(i=0;i<n-1;i++)
     {
             for(j=i+1;j<n;j++)
       {
      sx=lm[i].x+lm[i].vx*mid;
      sy=lm[i].y+lm[i].vy*mid;
      ex=lm[j].x+lm[j].vx*mid;
      ey=lm[j].y+lm[j].vy*mid;
      mx=lm[i].x+lm[i].vx*midmid;
      my=lm[i].y+lm[i].vy*midmid;
      nx=lm[j].x+lm[j].vx*midmid;
      ny=lm[j].y+lm[j].vy*midmid;
                        p1=maxi(p1,cnt(sx,sy,ex,ey));
      p2=maxi(p2,cnt(mx,my,nx,ny));
       }
     }
     if(p1>p2) l=mid;
     else r=midmid;
    }
    for(i=0;i<n-1;i++)
    {
     for(j=i+1;j<n;j++)
     {
      sx=lm[i].x+lm[i].vx*l;
      sy=lm[i].y+lm[i].vy*l;
      ex=lm[j].x+lm[j].vx*l;
      ey=lm[j].y+lm[j].vy*l;
      if(cnt(sx,sy,ex,ey)>ma) ma=cnt(sx,sy,ex,ey);
     }
    }
   printf("%.2lf %.2lf\n",l,ma);
  }
 return 0;
}
 

转载于:https://www.cnblogs.com/ffhuguang/p/3318978.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值