📜  安全警报启动的最短剩余时间

📅  最后修改于: 2022-05-13 01:56:06.819000             🧑  作者: Mango

安全警报启动的最短剩余时间

Geek 正在与N个骑自行车的人组织一场自行车比赛。第i骑车人的初始速度记为H i Km/hr ,第i骑车人的加速度记为A i Km/Hr 2 。速度为“L”或更高的骑自行车的人被认为是快速骑自行车的人。每小时在赛道上的总速度是通过将每个快速骑车人在该小时内的速度相加来计算的。当赛道上的总速度为“M”公里/小时或更高时,安全警报会打开。任务是找出安全警报启动的最小小时数。

例子:

方法:给定的问题可以通过使用二分搜索来解决,通过使用以下事实:如果自行车具有初始速度U并且具有均匀加速度A ,则可以使用以下等式找到任何时间点的速度: (V = U + A*t)并且如果在时间t满足条件,那么对于大于t的所有时间,将满足因此丢弃范围的右半部分,直到找到最小值。请按照以下步骤解决问题:

  • 定义一个函数check(long H[], long A[], long mid, long N, long M, long L)并执行以下步骤:
    • 初始化变量,比如sum0 ,它存储了速度的总和。
    • 使用变量i在范围[0, N]上迭代,如果(mid*A[i] + H[i])的值至少为L ,则将此值添加到sum
    • 执行上述步骤后,返回sum的值作为结果。
  • 初始化变量,例如010 10作为答案的二分搜索范围,以及存储最小小时数的0
  • 迭代直到low <= high并执行以下步骤:
    • 找到mid的值为(low + high)/2
    • 调用函数check(H, A, mid, N, M, L) ,如果函数返回的值至少为 M ,则将ans的值更新为mid 。否则,将high的值更新为(mid – 1)
    • 否则,将low的值更新为(mid + 1)
  • 执行上述步骤后,打印ans的值作为结果小时数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the value of
// mid as the minimum number of hours
// satisfies the condition
long check(long H[], long A[], long mid,
           long N, long M, long L)
{
    // Stores the sum of speed
    long sum = 0;
 
    // Iterate over the range [0, N]
    for (long i = 0; i < N; i++) {
 
        // Find the value of speed
        long speed = mid * A[i] + H[i];
 
        // If the bike is considered
        // to be fast add it in sum
        if (speed >= L) {
            sum += speed;
        }
    }
 
    // Return the resultant sum
    return sum;
}
 
// Function to find the minimum number
// of time required
long buzzTime(long N, long M, long L,
              long H[], long A[])
{
    // Stores the range of Binary Search
    long low = 0, high = 1e10;
 
    // Stores the minimum number of
    // time required
    long ans = 0;
 
    while (high >= low) {
 
        // Find the value of mid
        long mid = low + (high - low) / 2;
 
        // If the mid is the resultant
        // speed required
        if (check(H, A, mid,
                  N, M, L)
            >= M) {
 
            // Update the ans and high
            ans = mid;
            high = mid - 1;
        }
 
        // Otherwise
        else
            low = mid + 1;
    }
 
    // Return the minimum number of hours
    return ans;
}
 
// Driver Code
int main()
{
    long M = 400, L = 120;
    long H[] = { 20, 50, 20 };
    long A[] = { 20, 70, 90 };
    long N = sizeof(A) / sizeof(A[0]);
 
    cout << buzzTime(N, M, L, H, A);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
 
  // Function to check if the value of
  // mid as the minimum number of hours
  // satisfies the condition
  static long check(long H[], long A[], long mid,
                    long N, long M, long L)
  {
 
    // Stores the sum of speed
    long sum = 0;
 
    // Iterate over the range [0, N]
    for (long i = 0; i < N; i++) {
 
      // Find the value of speed
      long speed = mid * A[(int) i] + H[(int) i];
 
      // If the bike is considered
      // to be fast add it in sum
      if (speed >= L) {
        sum += speed;
      }
    }
 
    // Return the resultant sum
    return sum;
  }
 
  // Function to find the minimum number
  // of time required
  static long buzzTime(long N, long M, long L,
                       long H[], long A[])
  {
    // Stores the range of Binary Search
    long low = 0, high = 100000000;
 
    // Stores the minimum number of
    // time required
    long ans = 0;
 
    while (high >= low) {
 
      // Find the value of mid
      long mid = low + (high - low) / 2;
 
      // If the mid is the resultant
      // speed required
      if (check(H, A, mid,
                N, M, L)
          >= M) {
 
        // Update the ans and high
        ans = mid;
        high = mid - 1;
      }
 
      // Otherwise
      else
        low = mid + 1;
    }
 
    // Return the minimum number of hours
    return ans;
  }
 
  // Driver Code
  public static void main (String[] args) {
    long M = 400, L = 120;
    long H[] = { 20, 50, 20 };
    long A[] = { 20, 70, 90 };
    long N = A.length;
 
 
    System.out.println(buzzTime(N, M, L, H, A));
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 program for the above approach
 
# Function to check if the value of
# mid as the minimum number of hours
# satisfies the condition
def check(H, A, mid, N, M, L):
    # Stores the sum of speed
    sum = 0
 
    # Iterate over the range [0, N]
    for i in range(N):
        # Find the value of speed
        speed = mid * A[i] + H[i]
 
        # If the bike is considered
        # to be fast add it in sum
        if (speed >= L):
            sum += speed
 
    # Return the resultant sum
    return sum
 
# Function to find the minimum number
# of time required
def buzzTime(N, M, L, H, A):
    # Stores the range of Binary Search
    low = 0
    high = 1e10
 
    # Stores the minimum number of
    # time required
    ans = 0
 
    while (high >= low):
 
        # Find the value of mid
        mid = low + (high - low) // 2
 
        # If the mid is the resultant
        # speed required
        if (check(H, A, mid, N, M, L) >= M):
            # Update the ans and high
            ans = mid
            high = mid - 1
 
        # Otherwise
        else:
            low = mid + 1
 
    # Return the minimum number of hours
    return int(ans)
 
# Driver Code
if __name__ == '__main__':
    M = 400
    L = 120
    H = [20, 50, 20]
    A = [20, 70, 90]
    N = len(A)
 
    print(buzzTime(N, M, L, H, A))
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the value of
// mid as the minimum number of hours
// satisfies the condition
static long check(long []H, long []A, long mid,
                  long N, long M, long L)
{
 
    // Stores the sum of speed
    long sum = 0;
     
    // Iterate over the range [0, N]
    for(long i = 0; i < N; i++)
    {
         
        // Find the value of speed
        long speed = mid * A[(int) i] + H[(int) i];
         
        // If the bike is considered
        // to be fast add it in sum
        if (speed >= L)
        {
            sum += speed;
        }
    }
     
    // Return the resultant sum
    return sum;
}
 
// Function to find the minimum number
// of time required
static long buzzTime(long N, long M, long L,
                     long []H, long []A)
{
     
    // Stores the range of Binary Search
    long low = 0, high = 100000000;
     
    // Stores the minimum number of
    // time required
    long ans = 0;
     
    while (high >= low)
    {
     
        // Find the value of mid
        long mid = low + (high - low) / 2;
         
        // If the mid is the resultant
        // speed required
        if (check(H, A, mid, N, M, L) >= M)
        {
         
            // Update the ans and high
            ans = mid;
            high = mid - 1;
        }
         
        // Otherwise
        else
            low = mid + 1;
    }
     
    // Return the minimum number of hours
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    long M = 400, L = 120;
    long []H = { 20, 50, 20 };
    long []A = { 20, 70, 90 };
    long N = A.Length;
     
    Console.Write(buzzTime(N, M, L, H, A));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
3

时间复杂度: O(N*log(max(L, M)))
辅助空间: O(1)