The Moving Points -HDU 4717 三分

本文介绍了一道算法题目,要求通过三分法找到使多个移动点间的最大距离达到最小值的时间点及其具体距离。该问题可通过计算各点移动速度及位置来解决。

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

The Moving Points

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1192    Accepted Submission(s): 483


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


题意:找到一个时刻使每两个点的距离的最大值最小。

思路:三分这个时间,然后找到最小的最大值,因为这个值是一个二次函数,所以要用到三分。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{ double x,y,vx,vy;
}point[310];
int n;
double mi,mi2,ti,eps=1e-8;

double dis(int i,int j,double ti)
{ return sqrt( (point[i].x+point[i].vx*ti-point[j].x-point[j].vx*ti)*(point[i].x+point[i].vx*ti-point[j].x-point[j].vx*ti)
              +(point[i].y+point[i].vy*ti-point[j].y-point[j].vy*ti)*(point[i].y+point[i].vy*ti-point[j].y-point[j].vy*ti) );
}
double solve(double ti)
{ double ans=0;
  int i,j;
  for(i=1;i<n;i++)
   for(j=i+1;j<=n;j++)
    ans=max(ans,dis(i,j,ti));
  return ans;
}
int main()
{ int T,t,i,j;
  double l=0,r=1000000000,k;
  scanf("%d",&T);
  for(t=1;t<=T;t++)
  { scanf("%d",&n);
    for(i=1;i<=n;i++)
     scanf("%lf%lf%lf%lf",&point[i].x,&point[i].y,&point[i].vx,&point[i].vy);
    l=0,r=1000000000;
    while(r-l>eps)
    { k=(r-l)/3;
      mi=l+k;mi2=l+k*2;
      if(solve(mi)<solve(mi2))
       r=mi2;
      else
       l=mi;
    }
    printf("Case #%d: %.2f %.2f\n",t,l,solve(l));
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值