Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

SEBIHWY - Editorial

$
0
0

PROBLEM LINK:

Contest
Practice

Author:Istvan Nagy
Tester:Kevin Atienza
Translators:Sergey Kulik (Russian), Team VNOI (Vietnamese) and Hu Zecong (Mandarin)
Editorialist:Kevin Atienza

DIFFICULTY:

Cakewalk

PREREQUISITES:

Ad hoc

PROBLEM:

Sebi and his father are in a car, and they want to guess the speed of another car. Both cars travel at a constant speed, and the second is faster than the first car. There are markers on the highway every 50 meters.

They start a timer at the instant at which their car and the other car are parallel to each other. After $T$ seconds, they observe that both the cars are next to some markers and the number of markers between them is $D - 1$. The speed of Sebi's father's car is $S$.

Sebi and his father guess that the speed is $SG$ and $FG$, respectively. Determine who has the better guess.

QUICK EXPLANATION:

The correct speed of the other car is $S_{\text{other}} := S + \frac{180D}{T}$, in the right units. (Be careful: This speed isn't necessarily an integer.) Thus:

  • If $|SG - S_{\text{other}}| < |FG - S_{\text{other}}|$, the answer is SEBI.
  • If $|SG - S_{\text{other}}| > |FG - S_{\text{other}}|$, the answer is FATHER.
  • If $|SG - S_{\text{other}}| = |FG - S_{\text{other}}|$, the answer is DRAW.

EXPLANATION:

To answer the problem, we need to compute the speed of the other car exactly. Let's denote that speed by $S_{\text{other}}$, in kph. If we can do this, then answering the problem boils down to computing which one has the "better guess": we compute the "error" of Sebi's and his father's guess as:

  • $S_{\text{err}} = |SG - S_{\text{other}}|$
  • $F_{\text{err}} = |FG - S_{\text{other}}|$

Then:

  • If $S_{\text{err}} < F_{\text{err}}$, the answer is SEBI.
  • If $S_{\text{err}} > F_{\text{err}}$, the answer is FATHER.
  • If $S_{\text{err}} = F_{\text{err}}$, the answer is DRAW.

Computing $S_{\text{other}}$

We know how fast the first car travels, and that the second car is faster. Thus, we only need to know exactly how much faster the second car is than the first.

They start out in the same place, and then after $T$ seconds, they end up next to some markers. Since there are $D - 1$ markers between them, it means that after $T$ seconds, the cars are $50D$ meters away from each other. But the speeds of the cars are constant, which means this completely determines how much faster the second one is. Specifically, the second one is exactly $\frac{50D}{T}$ meters per second faster than the first!

Using this, and the fact that the first car has speed $S$ kph, we can simply add them to compute the speed of the second car, $S_{\text{other}}$. But we can't simply add them, because they are in different units! In order to add them properly and compare with $SG$ and $FG$, we need to convert $\frac{50D}{T}$ meters per second into kph. This can be done as follows:

$$\frac{\text{$50D$ m}}{\text{$T$ sec}} = \frac{\text{$50D$ m}}{\text{$T$ sec}} \frac{\text{$60$ sec}}{\text{$1$ min}} \frac{\text{$60$ min}}{\text{$1$ hr}} \frac{\text{$1$ km}}{\text{$1000$ m}} = \frac{\text{$180D$ km}}{\text{$T$ hr}}$$

This means that the second car is exactly $S_{\text{other}} := S + \frac{180D}{T}$ kph.

Here's an implementation: (in Python 3)

from fractions import Fraction as F
def solve():
    s, sg, fg, d, t = map(int, input().strip().split())
    actual = s + F(d*50*60*60, t*1000)
    sdist = abs(sg - actual)
    fdist = abs(fg - actual)
    return 'SEBI' if sdist < fdist else 'FATHER' if sdist > fdist else 'DRAW'


for cas in range(int(input())):
    print(solve())

Other possible problems

Here are a few possible bugs:

  • We need to divide $\frac{180D}{T}$ exactly. This means the data type for $S_{\text{other}}$ should not be an integer. You could use, for example, float or double. (The solution above uses fractions.) An alternative is to scale the speeds by $T$, so that they all become integers.
  • Even if you use a double, you might still get an issue if you write your code as follows: double S_other = 180*D / T; This is because even though S_other is a double, D and T might not be, so 180 * D / T might round off unexpectedly. To avoid this, you can

    • declare D and T to be doubles, or
    • use casting ((double) 180 * D / T), or
    • simply write it as 180.0 * D / T. Note that D / T * 180.0 might still fail. (Why?)

Time Complexity:

$O(1)$

AUTHOR'S AND TESTER'S SOLUTIONS:

setter
tester


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>