公式

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

GPT 5.2 High

Overview

This problem requires computing Takahashi’s “earliest arrival time” using a formula and comparing it with the last train’s station arrival time to determine Yes/No.

Analysis

Key Insight

Takahashi has speeds: - Normal: \(1\) - While dashing: \(D\)

and the total time he can dash is \(K\) seconds.

If the total time spent dashing up to time \(t\) seconds is \(x\) seconds (\(0 \le x \le \min(K,t)\)), the distance Takahashi can cover is

\[ ( t-x )\cdot 1 + x \cdot D = t + (D-1)x \]

Since we want to maximize the distance, it is optimal to make \(x\) as large as possible (= use dashing as much as possible). In other words, only “how many total seconds he dashes” matters, not “when he dashes”, so the earliest arrival time is uniquely determined by a formula.

Why a Naive Approach Fails

If we “simulate choosing whether to dash each second,” it could require over \(10^9\) operations in the worst case, which won’t finish in time (TLE).

Instead, we directly compute the earliest arrival time using case analysis.

The Last Train’s Side

The last train heads from coordinate \(M\) toward station \(G\), so the distance to the station is

\[ \text{dist\_train} = M-G \]

The last train’s arrival time at the station is

\[ T_{\text{train}} = \frac{M-G}{V} \]

Takahashi can board if his arrival time \(T_{\text{taka}}\) is at most this value (simultaneous arrival is also acceptable).

Algorithm

We compute Takahashi’s earliest arrival time \(T_{\text{taka}}\).

1) When dashing alone is enough to reach the station: \(D K \ge G\)

While dashing (\(t \le K\)), the distance covered is \(Dt\), so

\[ Dt \ge G \Rightarrow t \ge \frac{G}{D} \]

Therefore

\[ T_{\text{taka}} = \frac{G}{D} \]

The condition to check is $\( \frac{G}{D} \le \frac{M-G}{V} \)$

but to avoid decimals, we multiply both sides to get

\[ G \cdot V \le D \cdot (M-G) \]

2) When dashing for \(K\) seconds is not enough: \(D K < G\)

First, the distance covered by dashing for \(K\) seconds is \(DK\). It is optimal to cover the remaining distance at normal speed \(1\).

In this case, the maximum distance coverable in total time \(t\) (\(t \ge K\)) is

\[ t + (D-1)K \]

The condition to reach the station is

\[ t + (D-1)K \ge G \Rightarrow t \ge G - (D-1)K \]

Since \(DK < G\) implies \(G-(D-1)K > K\), the earliest time is simply

\[ T_{\text{taka}} = G - (D-1)K \]

The condition to check is

\[ G - (D-1)K \le \frac{M-G}{V} \]

Avoiding decimals:

\[ \left(G-(D-1)K\right)\cdot V \le (M-G) \]

With the above, we can output Yes/No in constant time.

Complexity

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

Implementation Notes

  • Avoid floating point (float) and use cross-multiplication for comparisons (to prevent precision errors).

    • Example: Compare \(\frac{a}{b}\le\frac{c}{d}\) as \(a d \le b c\).
  • Values can be large (up to \(10^{15}\) or products thereof), so in languages other than Python, be aware of possible overflow beyond 64 bits and use 128-bit integers if necessary.

  • The branching condition is D*K >= G (whether dashing alone is sufficient). The case \(K=0\) is handled naturally as well.

    Source Code

import sys

def main():
    G, M, D, K, V = map(int, sys.stdin.readline().split())

    dist_train = M - G  # distance for the train to reach the station

    if D * K >= G:
        # Takahashi can reach in time G/D
        ok = (G * V) <= (D * dist_train)
    else:
        # Use all K seconds of dash, then normal speed
        t_min = G - (D - 1) * K  # = K + (G - D*K)
        ok = (t_min * V) <= dist_train

    print("Yes" if ok else "No")

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.2-high.

投稿日時:
最終更新: