B - 廊下を往復するロボット / Robot Going Back and Forth in a Hallway Editorial by admin
DeepSeek V3Overview
This problem asks us to find the coordinates at time \(T\) of \(N\) robots that move back and forth along a corridor of length \(L\). Each robot moves while reflecting off walls, and their initial positions and velocities are given.
Analysis
Each robot’s motion is independent, and we need to account for reflections. Naively simulating the movement over time \(T\) would result in TLE (Time Limit Exceeded) since \(T\) can be as large as \(10^9\).
The key observation is that the motion after reflections is periodic. Each time a robot reflects off a wall, its direction of movement reverses, but we can consider that the robot returns to its original state after traveling a distance of \(2L\) (periodic motion with period 2). In other words, by considering the remainder when dividing the total travel distance by \(2L\), we can efficiently compute the final position.
Algorithm
For each robot, we compute the final position using the following procedure:
- If the velocity is 0: The robot doesn’t move, so we output the initial position \(x\) as is.
- If moving in the positive direction (\(v > 0\)):
- Let the total travel distance be \(d = T \times v\).
- If the initial position is 0:
- Compute the quotient (number of periods) and remainder when dividing \(d\) by \(L\).
- If the number of periods is even: the remainder is the coordinate directly.
- If the number of periods is odd: the coordinate is \(L\) minus the remainder.
- If the initial position is not 0:
- First, travel the distance \(L - x\) to the right wall.
- Compute the periodic motion with the remaining travel distance (same as above).
- If moving in the negative direction (\(v < 0\)):
- Let the total travel distance be \(d = T \times |v|\).
- If the initial position is \(L\):
- Compute the quotient and remainder when dividing \(d\) by \(L\).
- If the number of periods is even: the coordinate is \(L\) minus the remainder.
- If the number of periods is odd: the remainder is the coordinate directly.
- If the initial position is not \(L\):
- First, travel the distance \(x\) to the left wall.
- Compute the periodic motion with the remaining travel distance.
In this way, by using the remainder when dividing the travel distance by the corridor length \(L\), we handle reflections efficiently.
Complexity
- Time complexity: \(O(N)\)
- Because each robot can be computed in constant time.
- Space complexity: \(O(N)\)
- For storing the input and results.
Implementation Notes
We handle the cases separately depending on whether the initial position is on a wall (0 or \(L\)) or not.
In the periodic motion calculation, we determine the direction after reflection based on the parity of the quotient when dividing the travel distance by the corridor length.
Since only integer arithmetic is used, there is no concern about floating-point errors (the problem guarantees integer values).
Source Code
import sys
def main():
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
L = int(data[1])
T = int(data[2])
index = 3
robots = []
for i in range(n):
x = int(data[index])
v = int(data[index+1])
index += 2
robots.append((x, v))
results = []
for x, v in robots:
if v == 0:
results.append(x)
continue
if v > 0:
total_dist = T * v
if x == 0:
cycle = total_dist // L
rem = total_dist % L
if cycle % 2 == 0:
pos = rem
else:
pos = L - rem
else:
dist_to_right = L - x
if total_dist <= dist_to_right:
pos = x + total_dist
else:
total_dist -= dist_to_right
cycle = total_dist // L
rem = total_dist % L
if cycle % 2 == 0:
pos = L - rem
else:
pos = rem
else:
v_abs = -v
total_dist = T * v_abs
if x == L:
cycle = total_dist // L
rem = total_dist % L
if cycle % 2 == 0:
pos = L - rem
else:
pos = rem
else:
dist_to_left = x
if total_dist <= dist_to_left:
pos = x - total_dist
else:
total_dist -= dist_to_left
cycle = total_dist // L
rem = total_dist % L
if cycle % 2 == 0:
pos = rem
else:
pos = L - rem
results.append(pos)
for res in results:
print(res)
if __name__ == "__main__":
main()
This editorial was generated by deepseekv3.
posted:
last update: