C. Tram

Codeforces
IDCF746C
Time1000ms
Memory256MB
Difficulty
constructive algorithmsimplementationmath
English · Original
Chinese · Translation
Formal · Original
The tram in Berland goes along a straight line from the point 0 to the point _s_ and back, passing 1 meter per _t_1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points _x_ = 0 and _x_ = _s_. Igor is at the point _x_1. He should reach the point _x_2. Igor passes 1 meter per _t_2 seconds. Your task is to determine the minimum time Igor needs to get from the point _x_1 to the point _x_2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the point _x_1. Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is **not obligatory** that points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than 1 meter per _t_2 seconds). He can also stand at some point for some time. ## Input The first line contains three integers _s_, _x_1 and _x_2 (2 ≤ _s_ ≤ 1000, 0 ≤ _x_1, _x_2 ≤ _s_, _x_1 ≠ _x_2) — the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to. The second line contains two integers _t_1 and _t_2 (1 ≤ _t_1, _t_2 ≤ 1000) — the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter. The third line contains two integers _p_ and _d_ (1 ≤ _p_ ≤ _s_ - 1, _d_ is either 1 or ) — the position of the tram in the moment Igor came to the point _x_1 and the direction of the tram at this moment. If , the tram goes in the direction from the point _s_ to the point 0. If _d_ = 1, the tram goes in the direction from the point 0 to the point _s_. ## Output Print the minimum time in seconds which Igor needs to get from the point _x_1 to the point _x_2. [samples] ## Note In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds in total, because he passes 1 meter per 4 seconds. In the second example Igor can, for example, go towards the point _x_2 and get to the point 1 in 6 seconds (because he has to pass 3 meters, but he passes 1 meters per 2 seconds). At that moment the tram will be at the point 1, so Igor can enter the tram and pass 1 meter in 1 second. Thus, Igor will reach the point _x_2 in 7 seconds in total.
有轨电车在贝兰沿一条直线从点 #cf_span[0] 到点 #cf_span[s] 往返行驶,两个方向均以每 #cf_span[t1] 秒经过 #cf_span[1] 米的速度匀速直线运动。这意味着电车始终处于匀速直线运动状态,并在点 #cf_span[x = 0] 和 #cf_span[x = s] 处立即掉头。 Igor 位于点 #cf_span[x1],他需要到达点 #cf_span[x2]。Igor 以每 #cf_span[t2] 秒经过 #cf_span[1] 米的速度移动。 你的任务是确定 Igor 从点 #cf_span[x1] 到点 #cf_span[x2] 所需的最短时间,已知当 Igor 到达点 #cf_span[x1] 时,电车的位置和运动方向。 Igor 可以在任意时刻,只要他与电车位置重合,就无限制次数地进入电车。**不要求** Igor 上下车的点为整数。假设所有上下车行为瞬时完成。Igor 可以沿直线任意移动(但速度不超过每 #cf_span[t2] 秒 #cf_span[1] 米),也可以在某点停留一段时间。 第一行包含三个整数 #cf_span[s], #cf_span[x1] 和 #cf_span[x2](#cf_span[2 ≤ s ≤ 1000],#cf_span[0 ≤ x1, x2 ≤ s],#cf_span[x1 ≠ x2])——电车行驶的最大坐标、Igor 所在位置和目标位置。 第二行包含两个整数 #cf_span[t1] 和 #cf_span[t2](#cf_span[1 ≤ t1, t2 ≤ 1000])——电车经过 #cf_span[1] 米所需的时间(秒)和 Igor 经过 #cf_span[1] 米所需的时间(秒)。 第三行包含两个整数 #cf_span[p] 和 #cf_span[d](#cf_span[1 ≤ p ≤ s - 1],#cf_span[d] 为 #cf_span[1] 或 #cf_span[-1])——当 Igor 到达点 #cf_span[x1] 时,电车的位置和方向。若 #cf_span[d = -1],电车朝从点 #cf_span[s] 到点 #cf_span[0] 的方向行驶;若 #cf_span[d = 1],电车朝从点 #cf_span[0] 到点 #cf_span[s] 的方向行驶。 请输出 Igor 从点 #cf_span[x1] 到点 #cf_span[x2] 所需的最短时间(秒)。 在第一个例子中,Igor 步行比等待电车更划算。因此他需要移动 #cf_span[2] 米,总共耗时 #cf_span[8] 秒,因为他每 #cf_span[4] 秒移动 #cf_span[1] 米。 在第二个例子中,Igor 可以,例如,朝点 #cf_span[x2] 方向移动,在 #cf_span[6] 秒内到达点 #cf_span[1](因为他需要移动 #cf_span[3] 米,而他每 #cf_span[2] 秒移动 #cf_span[1] 米)。此时电车恰好到达点 #cf_span[1],因此 Igor 可以上车,并在 #cf_span[1] 秒内移动 #cf_span[1] 米。因此,Igor 总共用 #cf_span[7] 秒到达点 #cf_span[x2]。 ## Input 第一行包含三个整数 #cf_span[s], #cf_span[x1] 和 #cf_span[x2](#cf_span[2 ≤ s ≤ 1000],#cf_span[0 ≤ x1, x2 ≤ s],#cf_span[x1 ≠ x2])——电车行驶的最大坐标、Igor 所在位置和目标位置。第二行包含两个整数 #cf_span[t1] 和 #cf_span[t2](#cf_span[1 ≤ t1, t2 ≤ 1000])——电车经过 #cf_span[1] 米所需的时间(秒)和 Igor 经过 #cf_span[1] 米所需的时间(秒)。第三行包含两个整数 #cf_span[p] 和 #cf_span[d](#cf_span[1 ≤ p ≤ s - 1],#cf_span[d] 为 #cf_span[1] 或 #cf_span[-1])——当 Igor 到达点 #cf_span[x1] 时,电车的位置和方向。若 #cf_span[d = -1],电车朝从点 #cf_span[s] 到点 #cf_span[0] 的方向行驶;若 #cf_span[d = 1],电车朝从点 #cf_span[0] 到点 #cf_span[s] 的方向行驶。 ## Output 请输出 Igor 从点 #cf_span[x1] 到点 #cf_span[x2] 所需的最短时间(秒)。 [samples] ## Note 在第一个例子中,Igor 步行比等待电车更划算。因此他需要移动 #cf_span[2] 米,总共耗时 #cf_span[8] 秒,因为他每 #cf_span[4] 秒移动 #cf_span[1] 米。在第二个例子中,Igor 可以,例如,朝点 #cf_span[x2] 方向移动,在 #cf_span[6] 秒内到达点 #cf_span[1](因为他需要移动 #cf_span[3] 米,而他每 #cf_span[2] 秒移动 #cf_span[1] 米)。此时电车恰好到达点 #cf_span[1],因此 Igor 可以上车,并在 #cf_span[1] 秒内移动 #cf_span[1] 米。因此,Igor 总共用 #cf_span[7] 秒到达点 #cf_span[x2]。
**Definitions:** - Let $ s \in \mathbb{Z}^+ $ be the endpoint of the tram’s route, with $ 0 \leq x \leq s $. - Let $ x_1, x_2 \in [0, s] $, $ x_1 \ne x_2 $, be Igor’s start and target positions. - Let $ t_1 \in \mathbb{Z}^+ $ be the time (in seconds) for the tram to travel 1 meter. - Let $ t_2 \in \mathbb{Z}^+ $ be the time (in seconds) for Igor to travel 1 meter. - Let $ p \in [1, s-1] $ be the initial position of the tram when Igor starts at $ x_1 $. - Let $ d \in \{ -1, 1 \} $ be the initial direction of the tram: $ d = 1 $: moving from $ 0 \to s $, $ d = -1 $: moving from $ s \to 0 $. **Constraints:** - The tram moves with constant speed $ v_t = \frac{1}{t_1} $ m/s, bouncing instantaneously at $ x = 0 $ and $ x = s $. - Igor moves with maximum speed $ v_i = \frac{1}{t_2} $ m/s, can stand still, and can board/unboard the tram at any real point $ x \in [0, s] $ when his position coincides with the tram’s. - Igor may walk directly from $ x_1 $ to $ x_2 $, or use the tram (possibly multiple times), or a combination. **Objective:** Minimize the total time $ T \geq 0 $ for Igor to reach $ x_2 $ from $ x_1 $, given the tram’s initial state $ (p, d) $. --- **Formal Objective:** $$ \min_{T \geq 0} \left\{ T : \exists \text{ a path } \pi: [0, T] \to [0, s] \text{ s.t. } \pi(0) = x_1, \pi(T) = x_2, \text{ and } \forall t \in [0,T], \left| \frac{d\pi}{dt} \right| \leq \frac{1}{t_2}, \text{ and } \exists \text{ times } t_1^b, t_1^e, \dots, t_k^b, t_k^e \in [0,T] \text{ such that for } t \in [t_i^b, t_i^e], \pi(t) = \text{tram}(t) \right\} $$ where $ \text{tram}(t) $ is the position of the tram at time $ t $, defined by: - Initial position: $ \text{tram}(0) = p $ - Initial direction: $ d \in \{ -1, 1 \} $ - Velocity: $ v_t = d \cdot \frac{1}{t_1} $ - Reflection at boundaries: $$ \text{tram}(t) = \begin{cases} p + d \cdot \frac{t}{t_1} & \text{if no boundary hit} \\ \text{periodic reflection} & \text{otherwise} \end{cases} $$ The tram’s position at time $ t $ is given by the **sawtooth function**: Let $ \text{tram}(t) = \text{mod}(p + d \cdot \frac{t}{t_1}, 2s) $, then $$ \text{tram}(t) = \begin{cases} \text{mod}(p + d \cdot \frac{t}{t_1}, 2s) & \text{if } \text{mod}(p + d \cdot \frac{t}{t_1}, 2s) \leq s \\ 2s - \text{mod}(p + d \cdot \frac{t}{t_1}, 2s) & \text{otherwise} \end{cases} $$ Alternatively, define the **unfolded path** of the tram on the real line: Let $ \tilde{p}(t) = p + d \cdot \frac{t}{t_1} $. Then the actual tram position is: $$ \text{tram}(t) = s - \left| \left( \tilde{p}(t) \mod 2s \right) - s \right| $$ Igor’s position $ \pi(t) $ satisfies $ |\pi'(t)| \leq \frac{1}{t_2} $, and $ \pi(0) = x_1 $, $ \pi(T) = x_2 $. Igor can choose to walk the entire way: $$ T_{\text{walk}} = |x_2 - x_1| \cdot t_2 $$ Or he can wait for the tram to come to him at some time $ \tau \geq 0 $, such that $ \text{tram}(\tau) = \pi(\tau) $, and then ride it to $ x_2 $, possibly transferring multiple times. The minimal time is: $$ \boxed{ \min \left( |x_2 - x_1| \cdot t_2,\ \min_{\substack{\tau \geq 0 \\ \text{tram}(\tau) = \pi(\tau) \\ \pi(\tau) \in [0,s] \\ \pi(T) = x_2}} \left( \tau + \frac{|x_2 - \pi(\tau)|}{1/t_1} \right) \right) } $$ But since Igor can walk to meet the tram anywhere along the line, we must consider **all possible meeting times** $ \tau \geq 0 $ such that: - Igor can reach position $ \text{tram}(\tau) $ by time $ \tau $: $ |\text{tram}(\tau) - x_1| \leq \frac{\tau}{t_2} $ - Then ride the tram from $ \text{tram}(\tau) $ to $ x_2 $: time $ \frac{|x_2 - \text{tram}(\tau)|}{1/t_1} $ So total time: $$ T(\tau) = \tau + \frac{|x_2 - \text{tram}(\tau)|}{1/t_1} = \tau + t_1 \cdot |x_2 - \text{tram}(\tau)| $$ Subject to: $$ |\text{tram}(\tau) - x_1| \leq \frac{\tau}{t_2} $$ Thus, the minimal time is: $$ \boxed{ \min \left( |x_2 - x_1| \cdot t_2,\ \inf_{\tau \geq 0} \left\{ \tau + t_1 \cdot |x_2 - \text{tram}(\tau)| \ \middle|\ |\text{tram}(\tau) - x_1| \leq \frac{\tau}{t_2} \right\} \right) } $$ Where $ \text{tram}(\tau) $ is the position of the tram at time $ \tau $, defined by the periodic reflection dynamics from initial position $ p $ and direction $ d $.
Samples
Input #1
4 2 4
3 4
1 1
Output #1
8
Input #2
5 4 0
1 2
3 1
Output #2
7
API Response (JSON)
{
  "problem": {
    "name": "C. Tram",
    "description": {
      "content": "The tram in Berland goes along a straight line from the point 0 to the point _s_ and back, passing 1 meter per _t_1 seconds in both directions. It means that the tram is always in the state of uniform",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF746C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "The tram in Berland goes along a straight line from the point 0 to the point _s_ and back, passing 1 meter per _t_1 seconds in both directions. It means that the tram is always in the state of uniform...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "有轨电车在贝兰沿一条直线从点 #cf_span[0] 到点 #cf_span[s] 往返行驶,两个方向均以每 #cf_span[t1] 秒经过 #cf_span[1] 米的速度匀速直线运动。这意味着电车始终处于匀速直线运动状态,并在点 #cf_span[x = 0] 和 #cf_span[x = s] 处立即掉头。\n\nIgor 位于点 #cf_span[x1],他需要到达点 #cf_span[x2...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions:**\n\n- Let $ s \\in \\mathbb{Z}^+ $ be the endpoint of the tram’s route, with $ 0 \\leq x \\leq s $.\n- Let $ x_1, x_2 \\in [0, s] $, $ x_1 \\ne x_2 $, be Igor’s start and target positions.\n- Le...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments