C - 終電に間に合え! / Catch the Last Train! Editorial by admin Editorial for printing
Claude 4.5 OpusOverview
This is a problem where we compare the shortest time for Takahashi to arrive at the station by optimally using his full-speed dash, against the time the last train arrives at the station, to determine whether he can catch the last train.
Analysis
Problem Setup
- Takahashi: wants to move from coordinate \(0\) to coordinate \(G\)
- Last train: moving from coordinate \(M\) to coordinate \(G\) at speed \(V\)
- Takahashi has normal speed \(1\), and can dash at speed \(D\) (usable for up to \(K\) seconds)
Key Observations
The last train’s arrival time is fixed
The last train travels a distance of \(M - G\) at speed \(V\), so its arrival time is: $\(\text{Train arrival time} = \frac{M - G}{V}\)$
Finding Takahashi’s shortest arrival time
We consider the shortest time for Takahashi to travel distance \(G\). Since dashing makes him move faster, it is optimal to use as much full-speed dash as possible.
We consider two cases:
Case 1: He can arrive using only full-speed dash (\(D \times K \geq G\))
He can cover distance \(G\) using only full-speed dash. $\(\text{Shortest time} = \frac{G}{D}\)$
Case 2: Full-speed dash alone is not enough (\(D \times K < G\))
He dashes for \(K\) seconds covering \(D \times K\), then travels the remaining \(G - D \times K\) at normal speed \(1\). $\(\text{Shortest time} = K + (G - D \times K)\)$
Concrete Example
For \(G = 100\), \(D = 3\), \(K = 10\): - Distance covered by dashing: \(3 \times 10 = 30\) - Remaining distance: \(100 - 30 = 70\) - Shortest time: \(10 + 70 = 80\) seconds
Algorithm
- Calculate the time the last train arrives at the station: \(\text{train\_time} = \frac{M - G}{V}\)
- Calculate Takahashi’s shortest arrival time:
- If \(D \times K \geq G\): \(\text{takahashi\_time} = \frac{G}{D}\)
- Otherwise: \(\text{takahashi\_time} = K + (G - D \times K)\)
- If \(\text{takahashi\_time} \leq \text{train\_time}\), output
Yes; otherwise outputNo
Complexity
- Time complexity: \(O(1)\) (only arithmetic operations)
- Space complexity: \(O(1)\) (only variables used)
Implementation Notes
Beware of overflow: When computing \(D \times K\), if both \(D\) and \(K\) are at their maximum of \(10^9\), the product reaches \(10^{18}\). In Python there is no integer overflow so this is not an issue, but care is needed in other languages.
Floating-point comparison: This problem involves comparing results of division, but since the inputs are integers, it is possible to transform the inequality and compare using integers only (if a more precise implementation is needed).
Optimal use of full-speed dash: Since there is no reason to rest midway, the strategy of “using all full-speed dash at the beginning” is optimal.
Source Code
G, M, D, K, V = map(int, input().split())
# 終電が駅に到着する時刻
# 終電は座標Mから駅(座標G)へ向かう。距離は M - G
train_time = (M - G) / V
# 高橋君が駅に到着する最短時間
# 全力ダッシュをK秒使うと、その間は速度D、残りは速度1
# 全力ダッシュで進める距離: D * min(K, t) (t秒かけるとき、最大K秒まで全力ダッシュ可能)
# 通常移動で進める距離: 1 * max(0, t - K)
# 距離Gを移動するのに必要な最短時間を求める
# 全力ダッシュをk秒(k <= K)使うとする
# 全力ダッシュでD*k進み、残りG - D*kを通常速度1で進む
# 合計時間 = k + (G - D*k) = G - k*(D-1)
# もし全力ダッシュだけで到着できる場合(D*K >= G)
# 必要時間 = G / D (全力ダッシュのみ)
# より正確には、全力ダッシュをt秒すると距離D*tを移動
# G <= D*K なら、必要時間は G/D
if D * K >= G:
# 全力ダッシュだけで到着可能
takahashi_time = G / D
else:
# K秒全力ダッシュ + 残りを通常移動
# 全力ダッシュでD*K進み、残りG - D*Kを速度1で進む
takahashi_time = K + (G - D * K)
if takahashi_time <= train_time:
print("Yes")
else:
print("No")
This editorial was generated by claude4.5opus.
posted:
last update: