H - Closest Moment 解説
by
iastm
This solution assumes a basic understanding of vectors.
In this problem, Takahashi and Aoki always move in a straight line at constant speed. For linear motion, if a point starts at position \(\bm{s_0}\) and moves with constant velocity \(\bm{v}\), then its position at time \(t\geq0\) is \(\bm{s}(t)=\bm{s_0}+t\bm{v}.\)
We will view start and goal points \(\text{TS}, \text{TG}, \text{AS}, \text{AG}\) as two-dimensional vectors. Define \(t_\text{T}=|\text{TG}-\text{TS}|\) as the total time Takahashi spends walking, and define \(\bm{v}_\text{T}=\frac1{t_\text{T}}(\text{TG}-\text{TS})\) as Takahashi’s velocity. Similarly define \(t_\text{A}\) and \(\bm{v}_\text{A}\) as Aoki’s total time walking and Aoki’s velocity, respectively.
Since Takahashi walks in a straight line, his position vector \(\bm{s}_\text{T}(t)\) at time \(t\in[0, t_\text{T}]\) is given by the vector equation
\[\bm{s}_\text{T}(t)=\text{TS}+t\bm{v}_\text{T}.\]
Similarly, Aoki’s position vector \(\bm{s}_\text{A}(t)\) at time \(t\in[0, t_\text{A}]\) is given by
\[\bm{s}_\text{A}(t)=\text{AS}+t\bm{v}_\text{A}.\]
By symmetry, if \(t_\text{T}\gt t_\text{A}\), we can swap Takahashi and Aoki’s roles and assume \(t_\text{T}\leq t_\text{A}\).
As the problem suggests, Takahashi and Aoki do not necessarily stop at the same time. We can divide the problem into two time intervals:
- For \(t\in[0, t_\text{T}]\), both Takahashi and Aoki are walking towards their goal points.
- For \(t\in(t_\text{T}, t_\text{A}]\), Takahashi is stationary and Aoki is walking towards his goal point.
If \(t_\text{T}=t_\text{A}\), the second interval is empty, so the answer is the minimum distance in the first time interval. Otherwise, the answer is minimum distance among the two time intervals.
Consider the first time interval \(t\in[0, t_\text{T}]\). From Takahashi’s perspective, Aoki’s relative starting point is \((\text{AS}-\text{TS})\), and Aoki’s relative velocity is \((\bm{v}_\text{A}-\bm{v}_\text{T})\). Thus, Aoki’s relative position is
\[\bm{s}_\text{A}(t)-\bm{s}_\text{T}(t)=(\text{AS}-\text{TS})+t(\bm{v}_\text{A}-\bm{v}_\text{T}).\]
Since this equation describes linear motion, we can model Aoki’s relative movement as a line segment for \(t\in[0, t_\text{T}]\).
Next, consider the second time interval \(t\in(t_\text{T}, t_\text{A}]\). Since Takahashi stays at point \(\text{TG}\), we can model Aoki’s motion from the perspective of stationary point \(\text{TG}\). Aoki’s relative starting point is \((\text{AS}-\text{TG})\) and his relative velocity is \(\bm{v}_\text{A}\), so Aoki’s relative position is
\[\bm{s}_\text{A}(t)-\text{TG}=(\text{AS}-\text{TG})+t\bm{v}_\text{A}.\]
Since this equation similarly describes linear motion, we can again model Aoki’s relative movement as a line segment for \(t\in(t_\text{T}, t_\text{A}]\).
In both time intervals, we can rephrase the problem as finding the minimum distance between the origin and a given line segment.
Consider a line segment with distinct endpoints \(\bm{p}=(x_p, y_p)\) and \(\bm{q}=(x_q, y_q)\). Our goal is to find the minimum distance between the origin and the given line segment.
We can represent the line segment as a vector equation \(L(\lambda)=\bm{p}+\lambda(\bm{q}-\bm{p})\), giving us a point on the line segment for parameter \(\lambda\in[0, 1]\).
The distance between the origin and a point \(L(\lambda)\) is
\[|L(\lambda)|=\sqrt{(x_p+\lambda(x_q-x_p))^2+(y_p+\lambda(y_q-y_p))^2}.\]
To minimize \(|L(\lambda)|\), we can solve for \(\frac{\text{d}}{\text{d}\lambda}|L(\lambda)|=0\), but the derivative is slightly complicated. We instead utilize the fact that \(|L(\lambda)|\) is minimum whenever \(|L(\lambda)|^2\) is minimum, and use this to solve for \(\frac{\text{d}}{\text{d}\lambda}|L(\lambda)|^2=0\). Computing the derivative yields
\[ \begin{aligned} \frac{\text{d}}{\text{d}\lambda}|L(\lambda)|^2&=\frac{\text{d}}{\text{d}\lambda}\left((x_p+\lambda(x_q-x_p))^2+(y_p+\lambda(y_q-y_p))^2\right)\\ &=2(x_q-x_p)(x_p+\lambda(x_q-x_p))+2(y_q-y_p)(y_p+\lambda(y_q-y_p))\\ &=0 \end{aligned} \]
After simplifying, we obtain
\[\lambda=-\frac{x_p(x_q-x_p)+y_p(y_q-y_p)}{(x_q-x_p)^2+(y_q-y_p)^2}=-\frac{\bm{p}\cdot(\bm{q}-\bm{p})}{|\bm{q}-\bm{p}|^2}=\frac{\bm{p}\cdot(\bm{p}-\bm{q})}{|\bm{p}-\bm{q}|^2}\]
that minimizes \(|L(\lambda)|\).
Note that the above \(\lambda\) may not fall within interval \([0, 1]\). If \(\lambda<0\), the closest point to the origin is \(L(0)=\bm{p}\). If \(\lambda>1\), the closest point is \(L(1)=\bm{q}\).
Thus, we obtain a solution to the original problem. The time complexity is \(O(1)\).
投稿日時:
最終更新:
