📌  相关文章
📜  一个人必须移动的最小距离才能为每个赛车手拍照

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

一个人必须移动的最小距离才能为每个赛车手拍照

考虑一条直线跑道。一些赛车手在特定路段之间的赛道上奔跑。一个人正试图为每个赛车手拍照。如果该人站在赛跑者的跑道段之间的任何地方,即在赛跑者的起点和终点之间,则他可以拍摄赛跑者的照片。给定N个赛车手的起点和终点,以及一个整数D ,表示拍照人的初始位置。任务是找到该人必须移动的最小距离,以便从终点拍摄每个赛车手的照片。如果不可能为每个赛车手拍照而不是 print -1

例子:

方法:上述问题可以通过观察来解决,即在开始和完成比赛之前,该人必须领先于每个赛车手。因此,如果他在最后开始的赛车手和最先结束的赛车手的范围内,他可以拍照,否则不能。
在所有给定的赛车手中,找到起点的最大值说,终点的最小值说。现在,

  • left > right那么这个人不可能拍照和打印-1
  • 如果D[left, right]范围内,则该人不需要移动,答案将为0
  • 否则该人必须移动min(abs(left – D), abs(right – D))

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum
// distance the person has to move
// int order to get the pictures
int minDistance(int start[], int end[], int n, int d)
{
 
    // To store the minimum ending point
    int left = INT_MIN;
 
    // To store the maximum starting point
    int right = INT_MAX;
 
    // Find the values of minSeg and maxSeg
    for (int i = 0; i < n; i++) {
        left = max(left, start[i]);
        right = min(right, end[i]);
    }
 
    // Impossible
    if (left > right)
        return -1;
 
    // The person doesn't need to move
    if (d >= left && d <= right)
        return 0;
 
    // Get closer to the left point
    if (d < left)
        return (left - d);
 
    // Get closer to the right point
    if (d > right)
        return (d - right);
}
 
// Driver code
int main()
{
    int start[] = { 0, 2, 4 };
    int end[] = { 7, 14, 6 };
    int n = sizeof(start) / sizeof(int);
    int d = 3;
 
    cout << minDistance(start, end, n, d);
 
    return 0;
}


Java
// Java implementation for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to return the minimum
    // distance the person has to move
    // int order to get the pictures
    static int minDistance(int start[], int end[], int n,
                           int d)
    {
 
        // To store the minimum ending point
        int left = Integer.MIN_VALUE;
 
        // To store the maximum starting point
        int right = Integer.MAX_VALUE;
 
        // Find the values of minSeg and maxSeg
        for (int i = 0; i < n; i++) {
            left = Math.max(left, start[i]);
            right = Math.min(right, end[i]);
        }
 
        // Impossible
        if (left > right)
            return -1;
 
        // The person doesn't need to move
        if (d >= left && d <= right)
            return 0;
 
        // Get closer to the left point
        if (d < left)
            return (left - d);
 
        // Get closer to the right point
        if (d > right)
            return (d - right);
 
        return -1;
    }
 
  // Driver code
    public static void main(String[] args)
    {
        int start[] = { 0, 2, 4 };
        int end[] = { 7, 14, 6 };
        int n = start.length;
        int d = 3;
 
        System.out.println(minDistance(start, end, n, d));
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 program for the above approach
import sys
 
# Function to return the minimum
# distance the person has to move
# order to get the pictures
def minDistance(start, end, n, d) :
 
    # To store the minimum ending point
    left = -sys.maxsize
 
    # To store the maximum starting point
    right = sys.maxsize
 
    # Find the values of minSeg and maxSeg
    for i in range(n) :
        left = max(left, start[i])
        right = min(right, end[i])
     
 
    # Impossible
    if (left > right):
        return -1
 
    # The person doesn't need to move
    if (d >= left and d <= right):
        return 0
 
    # Get closer to the left point
    if (d < left) :
        return (left - d)
 
    # Get closer to the right point
    if (d > right) :
        return (d - right)
 
# Driver code
start = [ 0, 2, 4 ]
end = [ 7, 14, 6 ]
n = len(start)
d = 3
 
print(minDistance(start, end, n, d))
 
# This code is contributed by target_2.


C#
// C# program for above approach
using System;
 
class GFG{
 
// Function to return the minimum
// distance the person has to move
// int order to get the pictures
static int minDistance(int[] start, int[] end,
                       int n, int d)
{
     
    // To store the minimum ending point
    int left = Int32.MinValue;
 
    // To store the maximum starting point
    int right = Int32.MaxValue;
 
    // Find the values of minSeg and maxSeg
    for(int i = 0; i < n; i++)
    {
        left = Math.Max(left, start[i]);
        right = Math.Min(right, end[i]);
    }
 
    // Impossible
    if (left > right)
        return -1;
 
    // The person doesn't need to move
    if (d >= left && d <= right)
        return 0;
 
    // Get closer to the left point
    if (d < left)
        return (left - d);
 
    // Get closer to the right point
    if (d > right)
        return (d - right);
 
    return -1;
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] start = { 0, 2, 4 };
    int[] end = { 7, 14, 6 };
    int n = start.Length;
    int d = 3;
 
    Console.Write(minDistance(start, end, n, d));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出
1