懒惰的机器人 - 忽略命令

Lazy Robot - Ignoring Commands

在一条直线上,机器人被放置在位置0(即在t=0时刻,机器人被放置在0)。机器人收到 N 条移动命令。每个命令包含两个整数T和X,其中T代表机器人收到命令的时间,X代表直线上的目的地点。如果机器人收到命令,它开始以每秒 1 个单位的速度向目标点 X 移动,并在到达目标点时停止。机器人在移动时忽略命令。 N 个命令作为程序的输入传递。该程序必须打印机器人忽略的命令数作为输出。然后程序必须在处理完 N 个命令后打印机器人的位置

注意:所有命令总是根据时间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

我的程序:

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]

我理解问题陈述。但是我不知道这背后的逻辑。

首先你必须为当前时间和当前位置创建变量。
和变量来计算跳过的命令。

接下来您可以开始迭代命令,而忘记您的 t=1t=2t=3 等,因为您不必在新命令出现时检查它们来吧(因为机器人太懒了)但是你可以在你不动的时候检查它们(当你完成上一个命令时)

如果新命令的时间小于当前时间,那么您可以跳过它(并增加计数器)

如果新命令的时间大于(或等于)当前时间,那么您可以移动。您可以将命令时间设置为当前时间,计算新位置与当前时间之间的距离,并将此距离与当前时间相加,最后设置新位置。

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)

第一个列表的结果

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

第二个列表的结果

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

如果您发现此代码有任何错误!!!请post在这里

public class懒人机器人{

public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    int N=sc.nextInt();
    int pos=0,time=0,ignorecom=0;
    List<Integer> timing  = new ArrayList<>();
    List<Integer> dest = new ArrayList<>();
    for(int i=0;i<N;i++) {
        timing.add(sc.nextInt());
        dest.add(sc.nextInt());
    }
    pos = dest.get(0);
    time = timing.get(0)+Math.abs(pos);
    for(int i=1;i<N;i++) {
        if(timing.get(i)<time) {
            ignorecom++;                
            continue;
        }
        else if(time<=timing.get(i)) {
            time = timing.get(i);
            
            time = time+Math.abs(Math.abs(dest.get(i)+Math.abs(pos)));
            pos = dest.get(i);
            
        }
        
        
    }
    System.out.println(pos+" "+ignorecom);
    
}

}