懒惰机器人 - 忽略命令
在一条直线上,机器人被放置在位置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]
我理解了问题陈述。但我不知道这背后的逻辑。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
如果您发现此代码有任何错误!请在这里发布
公共类 LazyRobot {
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);
}
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
首先,您必须为当前时间和当前位置创建变量。
和变量来计算跳过的命令。
接下来,您可以开始迭代命令,并忘记您的
t=1
、t=2
、t=3
等,因为您不这样做不必在新命令到来时检查它们(因为机器人太懒了),但是您可以在不移动时检查它们(当您完成上一个命令时)如果新命令的时间低于当前时间,那么您可以跳过它(并增加计数器)
如果新命令的时间大于(或等于)当前时间,则你可以采取行动。您可以将命令时间设置为当前时间,计算新位置与当前时间之间的距离,并将该距离与当前时间相加,最后设置新位置。
第一个列表的结果
第二个列表的结果
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.
Result for first list
Result for second list