In a straight line, a robot is placed at position 0 (i.e., at the time t=0, the robot is placed at 0). The robot receives N moving commands. Each command contains two integers T and X, where T represents the time in which the robot receives the command and X represents the destination point on the straight line. If the robot receives a command, it starts moving towards the destination point X with the speed of 1 unit per second and it stops when it reaches the destination point. The robot ignores the commands when it is moving. The N commands are passed as the input to the program. The program must print the number of commands ignored by the robot as the output. Then the program must print the position of the robot after processing the N commands
Note: All commands are always given in chronological order based on the time T
Example Input/Output 1:
Input:
3
1 5
2 4
6 1
Output:
1
1
Explanation:
At t=0, the position of the robot is 0
At t = 1, the robot receives the 1st command and its position is 0
At t = 2, the position of the robot is 1 (The 2nd command is ignored by the robot as it is moving)
At t=3, the position of the robot is 2
At t = 4, the position of the robot is 3
At t=5, the position of the robot is 4
At t=6, the position of the robot is 5 and it receives the 3rd command
At t=7, the position of the robot is 4
At t=8, the position of the robot is 3
At t=9, the position of the robot is 2
At t=10, the postion of the robot is 1
Only one command is ignored by the robot. So 1 in the first line.
The final position of the robot is 1. So 1 is printed in the second line.
Example Input/Output 2:
Input:
6
1 -5
2 4
3 5
4 0
7 6
10 1
Output:
4
6
My program:
n=int(input())
pv=[]
for i in range(n):
a,b=map(int,input().split())
pv =[[a,b]]
a=min(pv)[0]
b=max(pv)[0]
I understood the problem statement. But I don't know the logic behind this.
CodePudding user response:
First you have to create variables for current time and current position.
And variable to count skiped commands.
Next you can start iterate commands, and forget about your t=1
, t=2
, t=3
, etc. because you don't have to check new commands at time when they come (because robot is too lazy for this) but you can check them when you not move (when you finished previous command)
If new command has time lower than current time then you can skip it (and increase counter)
If new command has time bigger (or equal) than current time then you can make move. You can set command time as current time, calculate distance between new position and current time, and add this distance to current time, and finally set new position.
commands = [[1,5], [2, 4], [6, 1]]
commands = [[1, -5], [2, 4], [3, 5], [4, 0], [7, 6], [10, 1]]
current_time = 1
current_pos = 0
skiped = 0
for t, p in commands:
print(f'time: {current_time:2} | pos: {current_pos:2} | cmd: {t:2}, {p:2}')
if t < current_time:
print('>>> skip')
skiped = 1
else:
distance = abs(current_pos - p)
print('>>> distance:', distance)
current_time = t distance
current_pos = p
print('----------------')
print('time:', current_time)
print('skip:', skiped)
print('pos :', current_pos)
Result for first list
time: 1 | pos: 0 | cmd: 1, 5
>>> distance: 5
time: 6 | pos: 5 | cmd: 2, 4
>>> skip
time: 6 | pos: 5 | cmd: 6, 1
>>> distance: 4
----------------
time: 10
skip: 1
pos : 1
Result for second list
time: 1 | pos: 0 | cmd: 1, -5
>>> distance: 5
time: 6 | pos: -5 | cmd: 2, 4
>>> skip
time: 6 | pos: -5 | cmd: 3, 5
>>> skip
time: 6 | pos: -5 | cmd: 4, 0
>>> skip
time: 6 | pos: -5 | cmd: 7, 6
>>> distance: 11
time: 18 | pos: 6 | cmd: 10, 1
>>> skip
----------------
time: 18
skip: 4
pos : 6