根据给定规则查找从点 0 到达点 N 所需的时间
给定两个正整数X和Y以及排列成一条直线的N个点,任务是根据以下规则找出从点 0到达点 N所需的时间:
- 每个点都有一个屏障,每Y分钟关闭一次,并在接下来的Y分钟内保持关闭。
- 如果屏障关闭,则到达某个点时,该人需要等到它打开。
- 从一个点移动到另一个点所花费的时间是X分钟。
- 最初,所有障碍都打开了。
例子:
Input: N = 5, X = 4, Y = 3
Output : 28
Explanation:
It takes 4 units of time to reach point number 1 from point number 0 and an additional wait for 2 minutes(till 4th unit of time the barrier was closed on 3 unit time and has to wait for 6th unit time to reopen). Therefore the total time for current hop is 6 unit.
Similarly, a total of 24 units of time to reach point 4 and wait there, and finally 4 units of time to reach point 28.
Input: N = 7, X = 6, Y = 2
Output: 54
方法:给定的问题可以基于以下观察来解决:
- 如果找到到达第一个点的时间,即N=1 ,或者第一个障碍何时出现,那么找到答案就变得容易了。
- 到达每个点时跟踪当前时间。如果到达一个城市时,障碍是打开的,那么移动到下一个点,否则等到障碍打开。
- 要检查障碍是否打开,请检查当前时间是否位于障碍打开或关闭的时间段内。
- 将当前时间除以Y ,如果在除以时间时,电流相等,则屏障打开,否则它们关闭。如果当前时间是奇数,则等到当前时间成为Y的下一个较大倍数,然后继续前进。
- 观察如果X
那么每次到达一个点时,等待屏障打开然后开始并重复该过程,因此答案将是(N – 1)*Y + X 。同样,对于Y>=X,当遇到第一个障碍时,计算到达该点所需的时间,并用它来计算到达N的总时间。
请按照以下步骤解决问题:
- 初始化变量,例如currtime为0 ,它存储所需的总时间。
- 使用变量i迭代范围[0, N]并执行以下步骤:
- 将变量check初始化为currtime/Y 。
- 如果check%2等于0 ,则将x的值添加到变量currtime 。
- 否则,将currtime的值增加(currtime+Y)/Y并将currtime乘以Y并将currtime * repeat + (N – (repeat * i)) * X的值添加到变量currtime和 break。
- 执行上述步骤后,打印currtime的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to get the total time to
// reach the point N
int getcurrtime(int N, int X, int Y)
{
// Store the answer
int currtime = 0;
// Iterate over the range
for (int i = 0; i < N; i++) {
int check = currtime / Y;
// If barrier is open
if (check % 2 == 0)
// Travel from that point
// to the next one
currtime += X;
else {
// Wait until it becomes the
// next greater integer of Y
currtime = (currtime + Y) / Y;
currtime = currtime * Y;
// Time to reach n point
int repeat = (N - 1) / i;
currtime = currtime * repeat
+ (N - (repeat * i)) * X;
break;
}
}
return currtime;
}
// Driver Code
int main()
{
int N = 7;
int X = 6;
int Y = 2;
cout << getcurrtime(N, X, Y);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to get the total time to
// reach the point N
static int getcurrtime(int N, int X, int Y)
{
// Store the answer
int currtime = 0;
// Iterate over the range
for (int i = 0; i < N; i++) {
int check = currtime / Y;
// If barrier is open
if (check % 2 == 0)
// Travel from that point
// to the next one
currtime += X;
else {
// Wait until it becomes the
// next greater integer of Y
currtime = (currtime + Y) / Y;
currtime = currtime * Y;
// Time to reach n point
int repeat = (N - 1) / i;
currtime = currtime * repeat
+ (N - (repeat * i)) * X;
break;
}
}
return currtime;
}
// Driver Code
public static void main(String[] args)
{
int N = 7;
int X = 6;
int Y = 2;
System.out.println(getcurrtime(N, X, Y));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python 3 program for the above approach
# Function to get the total time to
# reach the point N
def getcurrtime(N, X, Y):
# Store the answer
currtime = 0
# Iterate over the range
for i in range(N):
check = currtime / Y
# If barrier is open
if (check % 2 == 0):
# Travel from that point
# to the next one
currtime += X
else:
# Wait until it becomes the
# next greater integer of Y
currtime = (currtime + Y) / Y
currtime = currtime * Y
# Time to reach n point
repeat = (N - 1) / i
currtime = currtime * repeat + (N - (repeat * i)) * X
break
return int(currtime)
# Driver Code
if __name__ == '__main__':
N = 7
X = 6
Y = 2
print(getcurrtime(N, X, Y))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to get the total time to
// reach the point N
static int getcurrtime(int N, int X, int Y)
{
// Store the answer
int currtime = 0;
// Iterate over the range
for (int i = 0; i < N; i++) {
int check = currtime / Y;
// If barrier is open
if (check % 2 == 0)
// Travel from that point
// to the next one
currtime += X;
else {
// Wait until it becomes the
// next greater integer of Y
currtime = (currtime + Y) / Y;
currtime = currtime * Y;
// Time to reach n point
int repeat = (N - 1) / i;
currtime = currtime * repeat
+ (N - (repeat * i)) * X;
break;
}
}
return currtime;
}
// Driver Code
public static void Main(string[] args)
{
int N = 7;
int X = 6;
int Y = 2;
Console.WriteLine(getcurrtime(N, X, Y));
}
}
// This code is contributed by ukasp.
Javascript
输出:
54
时间复杂度: O(N)
辅助空间: O(1)