Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second. The other child is now at coordinate B, and she can travel the distance of W per second.
He can catch her when his coordinate is the same as hers. Determine whether he can catch her within T seconds (including exactly T seconds later). We assume that both children move optimally.
Constraints
-10^9 \leq A,B \leq 10^9
1 \leq V,W \leq 10^9
1 \leq T \leq 10^9
A \neq B
All values in
input are integers.
Input
Input is given from Standard Input in the following format:
A V
B W
T
Output
If "it" can catch the other child, print YES; otherwise, print NO.
Examples
Input
1 2
3 1
3
Output
YES
Input
1 2
3 2
3
Output
NO
Input
1 2
3 3
3
Output
NO
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given a problem: Two children on a number line. "It" is at coordinate A, speed V. Other child at B, speed W. Determine if "it" can catch the other within T seconds (inclusive). They move optimally. We assume both children move optimally, meaning they try to maximize their chances? Usually in pursuit-evasion, the pursuer tries to catch, evader tries to avoid. Both can move in either direction? On a number line, they can move left or right arbitrarily, I assume, with speed limits? Typically, in such problems, each child can move in any direction at their given speed (i.e., they can choose direction each instant, potentially change direction). But "optimally" might mean that both will choose their movement to either achieve capture or avoid capture. Since it's a simple line, we can reason about relative motion.
The problem: Determine if "it" can catch the other within T seconds. Both move optimally: "it" wants to minimize the time to catch, while the other wants to maximize or avoid being caught. If it's possible for "it" to guarantee catch within T regardless of evader's actions, or is it a question of existence of a strategy for "it" such that no matter how the other moves (within speed limit), capture occurs within T? Or does "both children move optimally" mean they both choose their paths to achieve their own goals: "it" tries to catch, evader tries to avoid, and we need to determine if "it" can catch under optimal play? That is typical for pursuit-evasion. However, the examples:
Example 1: A=1 V=2, B=3 W=1, T=3. Output YES.
Let's compute: If it moves toward B at speed 2, and the evader runs away? Since B is to the right of A, if the evader runs to the right, the distance initially is 2. The relative speed if both move in same direction: V - W = 1, so time to catch would be distance / (V - W) = 2/1 = 2 seconds, which is <=3, so YES. But if the evader runs towards the pursuer? That would cause faster catch, but evader wouldn't do that. So under optimal evasion, she would run away. So YES.
Example 2: A=1 V=2, B=3 W=2, T=3. Distance 2, relative speed if same direction: V - W = 0, so cannot catch if evader runs away. However, if evader runs towards, they meet earlier, but evader wouldn't do that. So under optimal evasion, cannot catch, output NO.
Example 3: A=1 V=2, B=3 W=3, T=3. Distance 2, relative speed V - W = -1 (pursuer slower). So cannot catch if evader runs away. So NO.
Thus, it seems we consider that the evader will try to avoid capture. So the question is: can the pursuer guarantee capture within T seconds regardless of the evader's actions (given both can change direction arbitrarily)? This is typical: on a line, with equal or lower max speed, pursuer cannot guarantee capture if evader runs away. If V > W, then pursuer can always eventually catch by moving directly towards evader's position? But the evader can also change direction. However, on a line, if the pursuer is faster, he can eventually catch no matter how the evader moves? Actually, there's a known result: on a line, if pursuer is strictly faster than evader, he can guarantee capture (by moving directly toward the evader). But need to consider that the evader might change direction, but the pursuer can adjust. However, the classic "hound and hare" on a line: if the hound runs directly at the hare with speed greater than the hare, the distance decreases at rate V - W*cos(theta) but here the hare can choose direction to maximize the angle. Actually, if both can move arbitrarily, the evader can keep the angle such that relative velocity component along line of sight is less than V - W? Let's think.
We have a one-dimensional line. Both can move left or right at any time. There is no constraint on acceleration or turning; they can instantly change direction. So the evader's strategy: She can always try to run away from the pursuer. But since it's a line, the direction "away" is well-defined: if the pursuer is to the left, she runs right; if pursuer to the right, she runs left. But can the pursuer ever force capture if V > W? Consider the pursuer always moves directly towards the evader's current position. Because it's one dimension, "directly towards" means he moves in the direction that reduces the absolute distance. If the evader is at a greater coordinate and runs away to the right, the pursuer runs right as well. The relative speed is V - W if they go same direction. If evader turns around and runs towards the pursuer, the relative speed becomes V + W and they meet sooner; so evader wouldn't do that. So the worst case for pursuer is evader always runs directly away. So the distance evolves as d' = - (V - W) if V > W? Actually, if both move along the line, we can define positions x_p(t), x_e(t). Their speeds are bounded by V and W respectively. The evader can choose velocity w(t) with |w| ≤ W, pursuer chooses v(t) with |v| ≤ V. The pursuer wants to make x_p = x_e at some time ≤ T, the evader wants to avoid that. This is a differential game. The value (capture time) can be computed.
Because it's 1D and both can change direction instantly, the optimal strategy for evader is to always run away from pursuer (maximizing separation). The optimal for pursuer is to always run towards evader (minimizing distance). This is intuitive. So the distance evolves according to: d' = -V + W if the evader runs away? Let's set: Suppose pursuer at x_p, evader at x_e, with x_e > x_p. Define distance d = x_e - x_p > 0. If pursuer moves right at speed V, and evader moves right at speed W, then d' = W - V = -(V - W). If V > W, d' negative => distance decreases. If V = W, distance constant if both move same direction. If V < W, distance increases. If evader moves left (toward pursuer), d' = -W - V? Actually if evader moves left (negative direction) at speed W, then her velocity is -W, pursuer moves right at +V, then d' = (-W) - V = -(V+W) => distance decreases faster. So evader would not do that. So the worst-case for pursuer is indeed moving away. So under optimal play, the distance changes at rate (W - V) when moving away. But is there any scenario where the evader can cause distance to decrease at a slower rate than (V - W) by zigzagging? In 1D, the direction is binary. If the evader changes direction, the pursuer can adapt. Let's think: Suppose at some moment, the evader is to the right. She runs right. Pursuer runs right. Distance decreases at rate V-W if V>W. Now if the evader suddenly reverses direction and runs left (toward pursuer), then the distance will start decreasing faster. But can that be used to delay capture? Possibly if the pursuer overshoots? But the pursuer can also reverse direction instantly. At the moment evader turns, the pursuer can detect and also turn. Let's analyze a discrete decision: If evader runs left, then they are moving towards each other. The distance decreases at rate V+W, which is faster capture. So it doesn't help evader. What if evader runs left and then right in a way that causes pursuer to waste time? But pursuer can always mirror the evader's direction to keep reducing distance? Actually, consider pursuer's strategy: always move towards the evader's current position. If the evader reverses, the pursuer will also reverse (since now the direction towards the evader is opposite). So the relative motion will always be such that the distance derivative is -V + (component of evader's velocity along line from pursuer to evader). If evader runs directly away, the component is +W (if we define positive direction from pursuer to evader), so d' = W - V = -(V-W). If evader runs directly toward, component is -W, so d' = -W - V = -(V+W). If evader runs perpendicular? Not possible in 1D. So the worst-case is indeed running away. So the minimum guaranteed rate of distance decrease (if V > W) is V - W. If V <= W, then if evader runs away, distance doesn't decrease (if V = W, stays same; if V < W, distance increases). Could evader ever cause distance to increase even if V > W? She could run away, but distance still decreases because V > W. So the distance will monotonically decrease if pursuer always moves towards. So capture time is d0/(V-W) if V > W. However, is it always possible for pursuer to always keep moving directly towards the evader? Since they're on a line, moving towards means moving in the direction that reduces the absolute distance. That's always feasible: at each instant, choose velocity sign = sign(x_e - x_p) * V (i.e., move towards). But note that the pursuer's maximum speed is V, so he can maintain that. The evader can also change direction instantly, but the pursuer can also change direction instantly. So the differential equation for distance d = |x_e - x_p|, assuming the pursuer moves towards, and evader chooses a direction (which may change). Then the time derivative of d is: d' = -V + w_proj, where w_proj is the component of evader's velocity along the line from pursuer to evader (taking sign such that positive means away). Since the evader can choose any w in [-W, W], the worst-case for pursuer (maximizing capture time) is w_proj = +W (if she runs away). So d' = -V + W = -(V-W). So indeed d(t) = d0 - (V-W) t, as long as V > W and the evader continues to run away. This holds until d reaches 0. So capture occurs at t = d0/(V-W). But is there any nuance: what if at some point before capture, the evader turns around and the pursuer might not instantly adjust? But we assume both can change direction instantly; the pursuer can react instantaneously, so d' instantly changes. However, if the evader turns around, the distance starts decreasing faster, which only helps the pursuer, so the evader wouldn't do that. So under optimal play, evader will always run directly away. So the condition for guaranteed capture within T is: if V > W, then initial distance d0 = |A-B| must satisfy d0/(V-W) ≤ T. If V <= W, then capture cannot be guaranteed (unless maybe initial distance zero, but A != B). However, is there any scenario where capture can still happen if V <= W? Possibly if the evader chooses a suboptimal path? But "both children move optimally" means each chooses the best strategy for their goal. So if V <= W, the evader can always avoid capture forever by simply running away. If V = W, and they start at different positions, distance stays constant if both move same direction; if pursuer moves towards and evader runs away, distance constant; so never catch. If pursuer tries some other strategy, like waiting or moving away to lure evader? But evader is optimal and wants to avoid capture; she would not approach. So capture impossible. So the answer is NO when V <= W. But wait, example 2: V=2, W=2, output NO, consistent. Example 3: V=2, W=3, output NO. So that matches.
Thus, the solution: compute d = abs(A - B). If V > W and d <= (V - W) * T, then YES else NO.
But is there any nuance about T seconds inclusive? The condition is d/(V-W) ≤ T, which includes equality.
Now check with example 1: d=2, V-W=1, t=2 ≤3 => YES.
Thus the solution seems straightforward.
But wait: Are there any other factors? The problem statement: "He can catch her when his coordinate is the same as hers. Determine whether he can catch her within T seconds (including exactly T seconds later). We assume that both children move optimally." The phrase "both children move optimally" could be interpreted differently: maybe both are cooperating to achieve the condition? Or maybe it means they choose their movements to maximize or minimize something? Usually in pursuit-evasion, "move optimally" means each tries to achieve their objective: the chaser tries to catch, the other tries to avoid. So our interpretation seems correct.
However, we must consider the possibility that they might not be able to change direction arbitrarily; maybe they must move in a straight line? But then "optimally" might refer to choosing a direction at start and moving at constant speed? Let's examine: If both must choose a direction at time 0 and then move at constant speed (no turning), then it's a simple intercept problem. In that case, we would determine if there exists a direction for the pursuer (and evader) such that capture occurs within T, given they both choose their directions optimally (the evader to avoid, pursuer to catch). But that would be a different problem. But the examples: For example 1, A=1 V=2, B=3 W=1, T=3. If both choose directions at start and move constantly, can the pursuer guarantee capture within 3 seconds? Let's analyze: Pursuer chooses a direction and speed (max V) constant; evader chooses direction and speed (max W) constant. Capture occurs if positions equal at some time ≤T. The pursuer wants to choose his direction to intercept, evader to avoid. With constant velocities, this becomes a pursuit problem: The pursuer can choose a velocity vector (in 1D, just sign) of magnitude ≤V; evader chooses velocity of magnitude ≤W. They start at positions A and B. After t seconds, positions: A + v_p * t, B + v_e * t. We ask if pursuer can choose v_p (with |v_p| ≤ V) such that for any v_e (with |v_e| ≤ W) there exists t ≤ T with A+v_p t = B+v_e t. That is, (A-B) + (v_p - v_e) t = 0 => t = (B-A)/(v_p - v_e) if denominator nonzero. Since evader can choose v_e to avoid this, pursuer must ensure that no matter v_e, there is t? That would be impossible if evader can pick v_e to make denominator zero or sign wrong. Alternatively, perhaps the interpretation is that both choose their paths optimally, but we want to know if there exists a scenario where he catches her, assuming both are rational and try to maximize/minimize. But the phrase "both children move optimally" might mean they will choose the best possible moves to achieve their goals, and we need to decide the outcome. That's exactly the game.
In constant velocity case, the evader could simply choose v_e = v_p (if possible) to keep relative distance constant, preventing capture if speeds are equal or if she can match his velocity. If V > W, she cannot match his speed exactly because she is limited to ≤W, but she can choose v_e = sign(v_p)*W? Actually, if pursuer chooses v_p, his speed magnitude is ≤V. Evader can choose any speed ≤W. If V > W, pursuer could choose v_p such that its magnitude is >W, then evader cannot match exactly. But she could choose direction opposite? Let's compute: Suppose A < B, initial distance d>0. Pursuer chooses velocity v_p (positive or negative). Evader chooses v_e. The condition for meeting: A + v_p t = B + v_e t => (v_p - v_e) t = B - A = d (if B > A). So t = d / (v_p - v_e). For t to be positive and finite, need v_p - v_e > 0. Evader can try to make v_p - v_e as small as possible (positive but close to zero) to make t large, or make it negative so that they never meet (if v_p - v_e < 0, then t would be negative, meaning they would have met in the past if they continued, but not in future). Actually, if v_p - v_e < 0, then the equation A + v_p t = B + v_e t gives (v_p - v_e) t = d > 0 => impossible because left side negative. So they never meet if v_p - v_e ≤ 0. So evader can avoid capture by choosing v_e such that v_e ≥ v_p (if d>0). Since she can choose any speed up to W, if she can choose v_e = v_p (if |v_p| ≤ W) then v_p - v_e =0, they never meet (distance constant). If v_p > W, then she cannot match v_p exactly, but she can choose v_e = W (maximum) but that might still be less than v_p, so v_p - v_e >0, so they would meet eventually. However, she could also choose v_e negative (run toward pursuer) which would make v_p - v_e even larger, causing faster meeting. So to avoid capture, she would want v_e as large as possible (in the same direction as v_p) to minimize v_p - v_e, ideally making it non-positive. Since v_p is positive (assuming he runs toward her), she will also run positive (away). So she will set v_e = +W (if she can). Then v_p - v_e = v_p - W. For capture to occur, need v_p > W and then t = d/(v_p - W). The pursuer can choose v_p up to V. To minimize t, he would set v_p = V. Then t = d/(V - W). So capture possible if d/(V-W) ≤ T. So the constant-velocity case yields the same condition, provided the pursuer chooses to run directly at the evader at max speed, and evader runs away at max speed. But what if the pursuer chooses a different direction? That would be suboptimal. So the result is same.
But the constant-velocity assumption might be more restrictive because they cannot change direction. But the problem says "move optimally" and doesn't specify that they can change direction. Typically, such problems allow them to change direction at any time. But both interpretations lead to same condition because the worst-case evasion is constant-speed away. Actually, if they can change direction, the pursuer can also adjust. But does that allow the pursuer to guarantee capture under more circumstances? Possibly if V <= W, maybe the pursuer could use some strategy like "head towards a point and then change" to trap evader? On a line, if pursuer is slower or equal speed, evader can always avoid capture indefinitely. Because evader can always keep distance constant by moving same direction at same speed, or increase distance if faster. If speeds equal, evader can maintain distance by moving away at same speed; if pursuer tries something else like moving opposite direction to lure, the evader can simply not approach. So capture impossible. So the condition stands.
Thus, answer is: if V > W and |A-B| <= (V - W) * T, print YES else NO.
But wait, is there any possibility that the pursuer can catch even if V <= W if T is small? For instance, if the evader runs towards the pursuer (maybe optimal for her is to avoid being caught, not to minimize catch time, so she wouldn't do that). So no.
Thus solution is simple.
Now double-check constraints: up to 1e9, multiplication (V-W)*T may overflow 32-bit but 64-bit is fine (since V,W,T up to 1e9, V-W up to 1e9, product up to 1e18, fits in 64-bit signed). |A-B| up to 2e9, so also okay.
Edge cases: A,B can be negative, but absolute difference works.
One more nuance: If V > W but T is huge, then capture possible if infinite time? But T is finite, so condition.
Now, I'll write final solution: read A,V; B,W; T. Compute d = abs(A-B). If V > W and d <= (V - W) * T then YES else NO.
But wait, what if V > W but d is zero? A != B by constraint, so d>0.
Alternatively, we can also compute needed time = d/(V-W) and compare with T using integer arithmetic to avoid floating point: d <= (V-W)*T.
Implementation straightforward.
But the problem statement: "We assume that both children move optimally." Could there be a twist: maybe the children are cooperating to end the game quickly? Unlikely.
Check sample 2: d=2, V-W=0, condition false => NO. Sample 3: d=2, V-W = -1 (negative) so V > W false => NO. So matches.
Thus solution accepted.
But wait, maybe there is a scenario where the pursuer can catch even if V <= W if the evader moves suboptimally? But "move optimally" means evader will not move suboptimally. So no.
Thus answer.
But I'm suspicious: Could there be a scenario where both can move optimally and the pursuer catches because the evader's optimal strategy is not simply to run away? For example, if T is very short, maybe the evader can't accelerate quickly? But we assume instantaneous changes. Also, maybe the evader might try to outsmart by moving towards the pursuer then turning, but that doesn't help.
Consider V > W. The evader runs away at speed W. The pursuer runs toward at speed V. Distance decreases at rate V-W, capture at time d/(V-W). If T >= that, capture. So YES. If T < that, then even under optimal pursuer strategy, evader can avoid capture for at least T seconds? Actually, if T is less than required time, then at time T the distance is d - (V-W)T >0, so they haven't met. But could there be some other pursuer strategy that catches earlier? Could he maybe not go directly toward, but try to intercept by predicting evader's movement? However, the evader is adaptive and can change direction. But perhaps if the pursuer moves in a way that cuts off the evader? On a line, there is no "cutting off"; the only way to meet is to have the same coordinate. If the pursuer moves directly towards, that's the fastest way to reduce distance. Any other strategy would result in slower distance reduction or even increase distance. So the minimum possible capture time is d/(V-W) when V>W. So if that is > T, then no guarantee of capture within T.
But is it possible that the evader could be forced to not run away at max speed because of some constraint? She can choose any speed up to W. So she can always run away at speed W. So the worst-case capture time for pursuer is indeed d/(V-W). So if d/(V-W) > T, then pursuer cannot guarantee capture within T. But maybe the problem asks: "Determine whether he can catch her within T seconds" might be interpreted as: Is there a movement strategy for the pursuer that results in capture within T, assuming the evader also moves optimally (to avoid)? That's what we did.
Thus answer is correct.
Now, is there any nuance about "including exactly T seconds later"? That's covered.
Thus final solution.
But before finalizing, I'll read the problem statement carefully: It says "Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second. The other child is now at coordinate B, and she can travel the distance of W per second.
He can catch her when his coordinate is the same as hers. Determine whether he can catch her within T seconds (including exactly T seconds later). We assume that both children move optimally."
Note: "he can travel the distance of V per second" likely means his maximum speed is V. Similarly for her. So they can move at any speed up to that, and can change direction arbitrarily.
Thus solution.
But I'll also think about potential pitfalls: In the constant velocity interpretation, if both must choose velocities at time 0 and maintain them, the condition is the same. So it's consistent.
Thus answer is simple.
Given the constraints, we must use 64-bit integer.
Implementation:
#include <bits/stdc++.h>
using namespace std;
int main() {
long long A, V, B, W, T;
cin >> A >> V >> B >> W >> T;
long long d = abs(A - B);
if (V > W && d <= (V - W) * T) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
But is there any case where V-W is negative? Then condition V > W fails, so output NO. So no issue.
One more nuance: The problem statement says "All values in input are integers." So we can do integer arithmetic.
Thus final answer.
I'll produce final response with explanation. The pursuer can catch the evader if and only if his speed is strictly greater than hers and the initial distance can be closed within the given time.
When the evader runs directly away at her maximum speed (W) and the pursuer runs directly toward her at his maximum speed (V), the distance decreases at rate (V - W).
The time needed to catch is (\frac{|A - B|}{V - W}).
If (V > W) and this time is ≤ T, then the answer is YES; otherwise NO.
cpp
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
long long A, V, B, W, T;
cin >> A >> V >> B >> W >> T;
long long d = llabs(A - B);
if (V > W && d <= (V - W) * T) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}