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
题意:
输入两个点坐标和速度向量
求这两个点能到的最近距离
输入
题解
求出距离表达式
在 [0,105] [ 0 , 10 5 ] 三分 t t 求的最小值即可
注意:在三分t的时候精度要高,答案要求为 10−6 10 − 6 但这是距离的精度,时间精度应该更大,这个题需要时间精度高于 10−10 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);
}