[ABC426E]Closest Moment题解
Time Limit: 3 sec / Memory Limit: 1024 MiB
Score : 450 points
Problem Statement
Takahashi and Aoki walk on a two-dimensional plane.
Takahashi's start point is (TSX,TSY) and goal point is (TGX,TGY). Aoki's start point is (ASX,ASY) and goal point is (AGX,AGY).
They simultaneously depart from their respective start points and walk straight toward their respective goal points at speed 1, and stop when they reach their respective goal points. (Note that they depart simultaneously, but they do not necessarily stop at the same time.)
Find the distance between them at the moment when the distance between them is shortest (including the moment they depart and after they stop).
Here, distance refers to Euclidean distance. That is, the distance between two points (x1,y1),(x2,y2) is defined as (x1−x2)2+(y1−y2)2.
You are given T test cases, so solve each of them.
有道 翻译
问题陈述
高桥和青木在一个二维平面上行走。
高桥的起点是 (TSX,TSY) ,终点是 (TGX,TGY) 。青木的起点为 (ASX,ASY) ,终点为 (AGX,AGY) 。
它们同时从各自的起点出发,以速度 1 向各自的目标点直线前进,到达各自的目标点时停止。(请注意,它们同时出发,但不一定同时停止。)
求它们之间距离最短的时刻(包括它们出发的时刻和它们停下来的时刻)的距离。
这里,距离指的是欧氏距离。即两点之间的距离 (x1,y1),(x2,y2) 定义为 (x1−x2)2+(y1−y2)2 。
给你 T 个测试用例,所以每个都解决。
Constraints
- 1≤T≤2×105
- −100≤TSX,TSY,TGX,TGY,ASX,ASY,AGX,AGY≤100
- (TSX,TSY)=(TGX,TGY)
- (ASX,ASY)=(AGX,AGY)
- All input values are integers.
有道 翻译
# #约束
—— 1≤T≤2×105
—— −100≤TSX,TSY,TGX,TGY,ASX,ASY,AGX,AGY≤100
—— (TSX,TSY)=(TGX,TGY)
—— (ASX,ASY)=(AGX,AGY)
—所有输入值均为整数。
Input
The input is given from Standard Input in the following format:
T case1 case2 ⋮ caseT
casei represents the i-th test case. Each test case is given in the following format:
TSX TSY TGX TGY ASX ASY AGX AGY
有道 翻译
# #输入
输入来自标准输入,格式如下:
T
case1
case2
⋮
caseT
casei 表示 i \第一个测试用例。每个测试用例以以下格式给出:
TSX TSY TGX TGY
ASX ASY AGX AGY
Output
Output T lines. The i-th line (1≤i≤T) should contain the answer for the i-th test case.
Your answer will be considered correct if the absolute or relative error from the true value is at most 10−6.
有道 翻译
# #输出
输出 T 行。 i \第一行 (1≤i≤T) 应该包含 i \第一个测试用例的答案。
如果与真实值的绝对或相对误差不超过 10−6 ,则认为您的答案是正确的。
Sample Input 1
Copy
4 0 0 -2 2 -1 -1 4 4 4 0 2 0 6 0 8 0 1 0 1 1 -1 0 1 1 -8 9 2 6 -10 -10 17 20
Sample Output 1
Copy
1.000000000000000 2.000000000000000 0.000000000000000 1.783905950993199
For the first test case, let time 0 be the moment they depart. Their behavior is as follows:
- Time 0: Takahashi departs from (0,0) and starts walking toward (−2,2) at speed 1. At the same time, Aoki departs from (−1,−1) and starts walking toward (4,4) at speed 1.
- Time 22: Takahashi reaches his goal point (−2,2) and stops. At this time, Aoki is at (1,1) and is still walking.
- Time 52: Aoki reaches his goal point (4,4) and stops.
The distance between them is shortest at time 21, when Takahashi and Aoki are at (−21,21) and (−21,−21) respectively, and the distance is 1.
For the second test case, the distance between them is shortest at the moment they depart, and the distance at that time is 2.
For the third test case, the distance between them is shortest after they both stop, and the distance at that time is 0.
有道 翻译
###输出示例
1.000000000000000
2.000000000000000
0.000000000000000
1.783905950993199
对于第一个测试用例,让time 0 为它们离开的时刻。它们的行为如下:
-时间 0 :高桥从 (0,0) 出发,以速度 1 走向 (−2,2) 。同时,青木从 (−1,−1) 出发,以 1 的速度向 (4,4) 方向走去。
-时间 22 :Takahashi到达他的目标点 (−2,2) 并停止。此时,青木在 (1,1) 处,仍在行走。
-时间 52 :青木到达他的目标点 (4,4) 并停止。
他们之间的距离在 21 时刻最短,高桥和青木分别在 (−21,21) 和 (−21,−21) ,距离为 1 。
对于第二个测试用例,在他们离开的时刻,他们之间的距离是最短的,此时的距离为 2 。
对于第三个测试用例,两者都停止后,两者之间的距离最短,此时的距离为 0 。
思路
分为都走与仅一个走两段,对于每段三分。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long t;
double tsx,tsy,tgx,tgy,asx,asy,agx,agy,eps=1e-10;
double tx,ty,ax,ay,lk=1000.000;
double abc(double xq,double yq,double xw,double yw){
return sqrt((xw-xq)*(xw-xq)+(yw-yq)*(yw-yq));
}
int main(){
cin>>t;
while(t--){
cin>>tsx>>tsy>>tgx>>tgy;
cin>>asx>>asy>>agx>>agy;
if(abc(tsx,tsy,tgx,tgy)>abc(asx,asy,agx,agy)){
swap(tsx,asx);
swap(tsy,asy);
swap(tgx,agx);
swap(tgy,agy);
}
tx=(tgx-tsx)*1.000/abc(tsx,tsy,tgx,tgy);
ty=(tgy-tsy)*1.000/abc(tsx,tsy,tgx,tgy);
ax=(agx-asx)*1.000/abc(asx,asy,agx,agy);
ay=(agy-asy)*1.000/abc(asx,asy,agx,agy);
double l=0.000,r=abc(tsx,tsy,tgx,tgy);
lk=1000.000;
while(l+eps<r){
double mdi=(r-l)/3.000;
double mid1=l+mdi;
double mid2=mid1+mdi;
if(abc(tsx+tx*mid1,tsy+ty*mid1,asx+ax*mid1,asy+ay*mid1)>abc(tsx+tx*mid2,tsy+ty*mid2,asx+ax*mid2,asy+ay*mid2)){
if(lk-abc(tsx+tx*mid2,tsy+ty*mid2,asx+ax*mid2,asy+ay*mid2)>eps){
lk=abc(tsx+tx*mid2,tsy+ty*mid2,asx+ax*mid2,asy+ay*mid2);
}
l=mid1;
}
else{
if(lk-abc(tsx+tx*mid1,tsy+ty*mid1,asx+ax*mid1,asy+ay*mid1)>eps){
lk=abc(tsx+tx*mid1,tsy+ty*mid1,asx+ax*mid1,asy+ay*mid1);
}
r=mid2;
}
//cout<<abc(tsx+tx*mid1,tsy+ty*mid1,asx+ax*mid1,asy+ay*mid1)<<" "<<abc(tsx+tx*mid2,tsy+ty*mid2,asx+ax*mid2,asy+ay*mid2)<<endl;
}
l=abc(tsx,tsy,tgx,tgy)+eps,r=abc(asx,asy,agx,agy);
//lk=1000.000;
while(l+eps<r){
double mdi=(r-l)/3.000;
double mid1=l+mdi;
double mid2=mid1+mdi;
if(abc(tgx,tgy,asx+ax*mid1,asy+ay*mid1)>abc(tgx,tgy,asx+ax*mid2,asy+ay*mid2)){
if(lk-abc(tgx,tgy,asx+ax*mid2,asy+ay*mid2)>eps){
lk=abc(tgx,tgy,asx+ax*mid2,asy+ay*mid2);
}
l=mid1;
}
else{
if(lk-abc(tgx,tgy,asx+ax*mid1,asy+ay*mid1)>eps){
lk=abc(tgx,tgy,asx+ax*mid1,asy+ay*mid1);
}
r=mid2;
}
}
printf("%.10lf\n",lk);
}
return 0;
}

浙公网安备 33010602011771号