公式

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

Gemini 3.0 Flash (Thinking)

Overview

This is a problem where you determine whether the shortest time for Takahashi to arrive at the station (coordinate \(G\)) by using his “full sprint” for a total of \(K\) seconds at optimal timing is less than or equal to the time the last train arrives at the station.

Analysis

To solve this problem, we need to compute “the time the last train arrives at the station” and “the shortest time for Takahashi to arrive at the station” separately, then compare them.

1. Time for the last train to arrive at the station \(T_M\)

The last train is currently at coordinate \(M\) and is heading toward coordinate \(G\) at a speed of \(V\) per second. The distance the last train needs to travel to reach the station is \(M - G\), so the arrival time is: $\(T_M = \frac{M - G}{V}\)$

2. Shortest time for Takahashi to arrive at the station \(T_T\)

Takahashi can use his full sprint (speed \(D\)) for up to \(K\) seconds. For the remaining time, he moves at normal speed (speed \(1\)). To minimize the time, it is best to cover as much distance as possible while moving at the faster speed. In other words, using the full sprint as much as possible (up to \(K\) seconds) is optimal.

Here, we need to consider two cases depending on whether the distance covered by sprinting \(K \times D\) is greater than or equal to the distance to the station \(G\).

  • Case A: \(K \times D \ge G\) He can reach the station by sprinting alone. The shortest time \(T_T\) is the distance \(G\) divided by the speed \(D\). $\(T_T = \frac{G}{D}\)$

  • Case B: \(K \times D < G\) Even sprinting for the full \(K\) seconds is not enough to reach the station, so he needs to walk the remaining distance at normal speed. The distance covered by sprinting is \(K \times D\), and the remaining walking distance is \(G - (K \times D)\). $\(T_T = K + \frac{G - (K \times D)}{1} = G - K(D - 1)\)$

3. Transforming the comparison (avoiding floating-point errors)

If we compute the condition \(T_T \le T_M\) directly, division may introduce decimal precision errors. In competitive programming, the standard approach is to clear the denominators and convert the comparison to use only integers.

  • Case A comparison: \(\frac{G}{D} \le \frac{M - G}{V} \iff G \times V \le (M - G) \times D\)
  • Case B comparison: \(G - K(D - 1) \le \frac{M - G}{V} \iff (G - K(D - 1)) \times V \le M - G\)

Algorithm

  1. Read the input values \(G, M, D, K, V\).
  2. Compare \(K \times D\) with \(G\) to determine Takahashi’s movement pattern for reaching the station.
  3. Based on the analysis above, perform comparison operations in the integer domain, and output Yes if the condition is satisfied, or No otherwise.

Complexity

  • Time complexity: \(O(1)\)
    • Only simple arithmetic operations and conditional branching are needed.
  • Space complexity: \(O(1)\)
    • No memory is used beyond variables to store the input values.

Implementation Notes

  • Handling large numbers: Since \(M\) can be as large as \(10^{15}\), in languages other than Python (which natively supports arbitrary-precision integers), you need to use 64-bit integer types (long long in C++).

  • Avoiding errors: As discussed in the analysis, by avoiding division (/) and using multiplication (*) for comparisons, floating-point precision errors can be completely prevented.

    Source Code

import sys

def solve():
    # 標準入力から全ての値を読み込む
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    # 各変数を整数として取得
    # G: 駅の座標
    # M: 終電の初期座標
    # D: 全力ダッシュ時の速度倍率
    # K: 全力ダッシュ可能時間(秒)
    # V: 終電の速度
    G = int(input_data[0])
    M = int(input_data[1])
    D = int(input_data[2])
    K = int(input_data[3])
    V = int(input_data[4])
    
    # 高橋君は座標0からGへ、終電は座標MからG(およびその先)へ向かう。
    # 終電が駅(座標G)に到着する時間を T_M とすると、 T_M = (M - G) / V。
    # 高橋君が駅(座標G)に到着する最短時間を T_T とすると、
    # 高橋君が終電に間に合う条件は T_T <= T_M である。
    # この条件式は、分母を払って V * T_T <= M - G と書き換えられる。
    
    # 高橋君の最短時間 T_T を求める:
    # 全力ダッシュを t 秒使うとすると、進む距離は t*D + (T_T - t)*1 = G。
    # これを変形すると T_T = G - t*(D - 1) となる。
    # T_T を最小化するには、ダッシュ時間 t を最大化すればよい。
    # ただし、制約として t <= K かつ t*D <= G (駅を通り過ぎない範囲でダッシュする)がある。
    # つまり、最適なダッシュ時間 t は min(K, G / D) である。
    
    if K * D >= G:
        # 全力ダッシュを t = G / D 秒使うことで、最短時間 T_T = G / D で駅に到達できる。
        # 判定式: V * (G / D) <= M - G  =>  V * G <= (M - G) * D
        if V * G <= (M - G) * D:
            print("Yes")
        else:
            print("No")
    else:
        # 全力ダッシュ可能時間を全て使い切り、残りの距離を通常速度で移動する。
        # t = K, T_T = G - K * (D - 1)
        # 判定式: V * (G - K * (D - 1)) <= M - G
        if V * (G - K * (D - 1)) <= M - G:
            print("Yes")
        else:
            print("No")

if __name__ == '__main__':
    solve()

This editorial was generated by gemini-3-flash-thinking.

投稿日時:
最終更新: