gym/226036-F. Two Points【三分查找】

F. Two Points

There are two points (x1, y1) and (x2, y2) on the plane. They move with the velocities (vx1, vy1) and (vx2, vy2). Find the minimal distance between them ever in future.

Input

The first line contains four space-separated integers x1, y1, x2, y2 ( - 104 ≤ x1,  y1,  x2,  y2 ≤ 104) — the coordinates of the points.
The second line contains four space-separated integers vx1, vy1, vx2, vy2 ( - 104 ≤ vx1,  vy1,  vx2,  vy2 ≤ 104) — the velocities of the points.

Output

Output a real number d — the minimal distance between the points. Absolute or relative error of the answer should be less than 10 - 6.

Examples

Input

1 1 2 2
0 0 -1 0

Output

1.000000000000000

Input

1 1 2 2
0 0 1 0

Output

1.414213562373095


题意:

输入两个点坐标和速度向量
求这两个点能到的最近距离
输入

x1,y1,x2,y2vx1,vy1,vx2,vy2 x 1 , y 1 , x 2 , y 2 v x 1 , v y 1 , v x 2 , v y 2


题解

求出距离表达式

d=(x1x2+t×(vx1vx2))2+(y1y2+t×(vy1vy2))2 d = ( x 1 − x 2 + t × ( v x 1 − v x 2 ) ) 2 + ( y 1 − y 2 + t × ( v y 1 − v y 2 ) ) 2

[0,105] [ 0 , 10 5 ] 三分 t t d的最小值即可
注意:在三分t的时候精度要高,答案要求为 106 10 − 6 但这是距离的精度,时间精度应该更大,这个题需要时间精度高于 1010 10 − 10
也可以不管精度,直接跑一百遍

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
double X1, Y1, x2, y2, vx1, vy1, vx2, vy2;
double pow2(double x) { return x * x; }
double check(double t)
{
    return  pow2(X1 - x2 + t * (vx1 - vx2)) + pow2(Y1 - y2 + t * (vy1 - vy2));
}
int main()
{
    scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &X1, &Y1, &x2, &y2, &vx1, &vy1, &vx2, &vy2);
    double L, R;
    L = 0;
    R = 1e6;
    int tot = 100;
    while (tot--)
    {
        double mid1 = (L + L + R) / 3;
        double mid2 = (R + R + L) / 3;
        double ans1 = check(mid1);
        double ans2 = check(mid2);
        if (ans1 <= ans2)R = mid2;
        else L = mid1;
    }
    double ans = sqrt(check(L));
    printf("%.9f\n", ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值