📜  让 K 辆汽车按时到达目的地所需的最低交换次数

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

让 K 辆汽车按时到达目的地所需的最低交换次数

给定N辆汽车和一个整数D 目的地的距离。所有的汽车都从同一个起点出发,朝着同一个目的地前进。每辆车的速度由数组speed[]给出,它们各自的位置递增顺序在数组position[] 中给出。一辆比前一辆速度慢的汽车被认为是另一辆汽车的障碍物。任务是在给定的时间T内以最少交换次数让K辆汽车到达目的地。一辆汽车只能与它后面的汽车交换,这只能用一辆车来完成。
一次以上互换,但每次互换应计为单独互换。如果K辆汽车无法在给定时间内到达目的地,则返回 -1

例子

方法:可以使用贪婪方法来解决任务。由于每辆车的位置是按递增顺序给出的,最好的方法是从最后一辆车开始迭代,为能及时到达目的地的汽车保持一个计数器可达,为无法到达的汽车保持一个计数器障碍物及时到达目的地。如果汽车可以及时到达,则将障碍物的数量添加到交换数量中。
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to get minimum
// number of swaps and returns
// number of cars that
// car reach there in time.
int reachables(int x[], int v[], int n,
               int& swaps, int d,
               int t, int k)
{
 
    // Counter for number of cars that
    // can reach in time
    int reachable = 0;
 
    // Counter for cars that cannot reach
    // destination in time.
    int obstacle = 0;
 
    int i = n - 1;
    while (i >= 0) {
 
        int temp = d - x[i];
        if (v[i] * t >= temp) {
            // If a car can reach in
            // time then increase the
            // reachable counter
            reachable++;
 
            // Adding the number of cars
            // that need to be swapped
            swaps += obstacle;
        }
        else {
            // If a car cannot reach
            // in time then increase
            // the obstacle counter
            obstacle++;
        }
 
        if (reachable >= k) {
            break;
        }
 
        i--;
    }
    // Return the swaps
    if (reachable >= k)
        return swaps;
    else
        return -1;
}
// Driver Code
int main()
{
    int n = 5, k = 3, d = 10, t = 5;
    int x[] = { 0, 2, 3, 5, 7 };
    int v[] = { 2, 1, 1, 1, 4 };
    int swaps = 0;
 
    cout << reachables(x, v, n, swaps, d, t, k);
    return 0;
}


Java
// Java program for the above approach
class GFG
{
  // Function to get minimum
  // number of swaps and returns
  // number of cars that
  // car reach there in time.
  static int reachables(int x[], int v[], int n,
                 int swaps, int d,
                 int t, int k)
  {
 
      // Counter for number of cars that
      // can reach in time
      int reachable = 0;
 
      // Counter for cars that cannot reach
      // destination in time.
      int obstacle = 0;
 
      int i = n - 1;
      while (i >= 0) {
 
          int temp = d - x[i];
          if (v[i] * t >= temp) {
              // If a car can reach in
              // time then increase the
              // reachable counter
              reachable++;
 
              // Adding the number of cars
              // that need to be swapped
              swaps += obstacle;
          }
          else {
              // If a car cannot reach
              // in time then increase
              // the obstacle counter
              obstacle++;
          }
 
          if (reachable >= k) {
              break;
          }
 
          i--;
      }
      // Return the swaps
      if (reachable >= k)
          return swaps;
      else
          return -1;
  }
   
  // Driver Code
  public static void main(String [] args)
  {
      int n = 5, k = 3, d = 10, t = 5;
      int x[] = { 0, 2, 3, 5, 7 };
      int v[] = { 2, 1, 1, 1, 4 };
      int swaps = 0;
 
      System.out.println(reachables(x, v, n, swaps, d, t, k));
 
  }
 
}
 
// This code is contributed by AR_Gaurav


C#
// C# program for the above approach
using System;
public class GFG
{
   
  // Function to get minimum
  // number of swaps and returns
  // number of cars that
  // car reach there in time.
  static int reachables(int []x, int []v, int n,
                 int swaps, int d,
                 int t, int k)
  {
 
      // Counter for number of cars that
      // can reach in time
      int reachable = 0;
 
      // Counter for cars that cannot reach
      // destination in time.
      int obstacle = 0;
 
      int i = n - 1;
      while (i >= 0) {
 
          int temp = d - x[i];
          if (v[i] * t >= temp) {
              // If a car can reach in
              // time then increase the
              // reachable counter
              reachable++;
 
              // Adding the number of cars
              // that need to be swapped
              swaps += obstacle;
          }
          else {
              // If a car cannot reach
              // in time then increase
              // the obstacle counter
              obstacle++;
          }
 
          if (reachable >= k) {
              break;
          }
 
          i--;
      }
      // Return the swaps
      if (reachable >= k)
          return swaps;
      else
          return -1;
  }
   
  // Driver Code
  public static void Main(string [] args)
  {
      int n = 5, k = 3, d = 10, t = 5;
      int []x = { 0, 2, 3, 5, 7 };
      int []v = { 2, 1, 1, 1, 4 };
      int swaps = 0;
 
      Console.WriteLine(reachables(x, v, n, swaps, d, t, k));
  }
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to get minimum
# number of swaps and returns
# number of cars that
# car reach there in time.
def reachables(x, v, n,swaps, d, t, k) :
 
    # Counter for number of cars that
    # can reach in time
    reachable = 0;
 
    # Counter for cars that cannot reach
    # destination in time.
    obstacle = 0;
 
    i = n - 1;
    while (i >= 0) :
 
        temp = d - x[i];
        if (v[i] * t >= temp) :
            # If a car can reach in
            # time then increase the
            # reachable counter
            reachable += 1;
 
            # Adding the number of cars
            # that need to be swapped
            swaps += obstacle;
             
        else :
            # If a car cannot reach
            # in time then increase
            # the obstacle counter
            obstacle += 1;
 
        if (reachable >= k) :
            break;
 
        i -= 1;
         
    # Return the swaps
    if (reachable >= k) :
        return swaps;
    else :
        return -1;
 
# Driver Code
if __name__ == "__main__" :
     
    n = 5; k = 3; d = 10; t = 5;
    x = [ 0, 2, 3, 5, 7 ];
    v = [ 2, 1, 1, 1, 4 ];
    swaps = 0;
 
    print(reachables(x, v, n, swaps, d, t, k));
 
    # This code is contributed by AnkThon


Javascript


输出
2

时间复杂度:O(N)
辅助空间:O(1)