公式

C - 終電に間に合え! / Catch the Last Train! 解説 by admin

Qwen3-Coder-480B

Overview

This is a problem where we determine whether Takahashi can make it in time for the last train. Takahashi can “sprint at full speed” for a limited amount of time, and moves at normal speed otherwise. If Takahashi can arrive at the station before the last train arrives, output Yes; otherwise, output No.

Analysis

In this problem, we need to find the earliest time Takahashi can arrive at the station by optimally using his full-speed sprint, and determine whether that time is at most the arrival time of the last train.

Key Observations

  • Takahashi’s travel time depends on the number of seconds \(k\) he uses the full-speed sprint.
  • To arrive at the station as early as possible, it is optimal to use the full-speed sprint as much as possible.
  • The effect of sprinting is very large (up to \(D\) times faster), and he can use it for up to \(K\) seconds, so if he can arrive using only sprinting, he should do so.

What about a naive approach?

For example, an approach that exhaustively searches over the number of seconds to use the full-speed sprint would result in TLE since \(K\) is on the order of \(10^9\). Therefore, we need to compute the answer mathematically and directly.

Solution

The minimum time \(T_{\text{takahashi}}\) for Takahashi to arrive at the station can be computed as follows:

  • If \(K \geq G\), he can arrive entirely by sprinting: $\( T_{\text{takahashi}} = \frac{G}{D} \)$

  • Otherwise:

    • Sprint for at most \(K\) seconds → covers distance \(K \cdot D\)
    • Walk the remaining distance \(G - K \cdot D\) at normal speed (1 distance per second) → time \(G - K \cdot D\)

However, if sprinting alone covers the distance (\(K \cdot D \geq G\)), then similarly: $\( T_{\text{takahashi}} = \frac{G}{D} \)$

The time for the last train to arrive at the station is: $\( T_{\text{train}} = \frac{M - G}{V} \)$

Finally, we determine whether \(T_{\text{takahashi}} \leq T_{\text{train}}\) to get the answer.

Example

Sample input:

G=10, M=30, D=3, K=2, V=2
  • Last train arrival time: \((30 - 10)/2 = 10\)
  • Takahashi sprints for 2 seconds covering \(2 \times 3 = 6\), then walks the remaining 4, so total time is \(2 + 4 = 6\)
  • \(6 \leq 10\), so Yes

Algorithm

  1. Calculate the last train’s arrival time at the station:
    $\( T_{\text{train}} = \frac{M - G}{V} \)$
  2. Calculate Takahashi’s minimum arrival time:
    • If \(K \geq G\), then \(T_{\text{takahashi}} = G/D\)
    • Otherwise:
      • If the sprint distance is at least \(G\), then similarly \(G/D\)
      • Otherwise, \(K + (G - K \cdot D)\)
  3. If \(T_{\text{takahashi}} \leq T_{\text{train}}\), output Yes; otherwise, output No

Complexity

  • Time complexity: \(O(1)\)
  • Space complexity: \(O(1)\)

Implementation Notes

  • When using floating-point numbers, care must be taken with comparison errors, but in this case we only need magnitude comparisons with sufficient precision, so there is no issue.

  • By handling the \(K \geq G\) case first, the subsequent calculations can be simplified.

  • Don’t forget to check the case where Takahashi can arrive using sprinting alone.

    Source Code

G, M, D, K, V = map(int, input().split())

# 終電が駅に到着する時間
T_train = (M - G) / V

# 高橋君が全力ダッシュをすべて使い切っても到着できるか?
# 全力ダッシュをK秒間使い、残り(G - K)秒を通常速度で進む場合の時間
# ただし、KがGより大きい場合は、全部ダッシュでG秒で到着可能
if K >= G:
    # 全部ダッシュで到着可能
    T_takahashi = G / D
else:
    # K秒ダッシュ + (G - K)秒通常速度
    distance_dashed = K * D
    if distance_dashed >= G:
        # ダッシュだけで到着可能
        T_takahashi = G / D
    else:
        # ダッシュ + 通常速度
        T_takahashi = K + (G - K * D)

# 高橋君が終電に間に合う条件:T_takahashi <= T_train
if T_takahashi <= T_train:
    print("Yes")
else:
    print("No")

This editorial was generated by qwen3-coder-480b.

投稿日時:
最終更新: