📌  相关文章
📜  找到一个使曼哈顿距离之和最小的点

📅  最后修改于: 2021-04-29 07:18:45             🧑  作者: Mango

给定K维空间中的N个点, 2\leq N\leq 10^{5}1\leq K\leq 5 。任务是确定点,以使从该点到N个点的曼哈顿距离之和最小。
曼哈顿距离是沿轴线成直角测量的两个点之间的距离。在一个平面上, p1(x1,y1)p2(x2,y2)的平面上,它是| x1 – x2 |。 + | y1 – y2 |

例子:

方法:为了尽量减少曼哈顿距离我们所要做的是只是在排序所有K尺寸和输出所述K尺寸的中间元素的点。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to print the required points which
// minimizes the sum of Manhattan distances
void minDistance(int n, int k, vector >& point)
{
  
    // Sorting points in all k dimension
    for (int i = 0; i < k; ++i)
        sort(point[i].begin(), point[i].end());
  
    // Output the required k points
    for (int i = 0; i < k; ++i)
        cout << point[i][(ceil((double)n / 2) - 1)] << " ";
}
  
// Driver code
int main()
{
  
    int n = 4, k = 4;
    vector > point = { { 1, 5, 2, 4 },
                                   { 6, 2, 0, 6 },
                                   { 9, 5, 1, 3 },
                                   { 6, 7, 5, 9 } };
  
    // function call to print required points
    minDistance(n, k, point);
  
    return 0;
}


Java
// Java implementation of above approach 
import java.util.Arrays;
  
class GFG 
{
  
// Function to print the required 
// points which minimizes the sum 
// of Manhattan distances
static void minDistance(int n, int k,
                        int point[][])
{
      
    // Sorting points in all k dimension 
    for (int i = 0; i < k; i++)
        Arrays.sort(point[i]);
      
    // Output the required k points
    for (int i = 0; i < k; i++)
        System.out.print(point[i][(int)
               Math.ceil((double)(n / 2) - 1)] + " ");
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 4;
    int k = 4;
    int point[][] = { { 1, 5, 2, 4 }, 
                      { 6, 2, 0, 6 }, 
                       { 9, 5, 1, 3 }, 
                      { 6, 7, 5, 9 } };
      
    // function call to print required points
    minDistance(n, k, point);
}
}
  
// This code is contributed by Bilal


Python
# Python implementation of above approach
  
# Function to print the required points which
# minimizes the sum of Manhattan distances
def minDistance(n, k, point):
  
    # Sorting points in all dimension
    for i in range(k):
        point[i].sort()
  
    # Output the required k points
    for i in range(k):
        print(point[i][((n + 1) // 2) - 1], end =" ")
  
  
# Driver code
n = 4
k = 4
point = [[1, 5, 2, 4],
         [6, 2, 0, 6],
         [9, 5, 1, 3],
         [6, 7, 5, 9]]
  
# function call to print required points
minDistance(n, k, point)


C#
// C# implementation of above approach 
using System;
  
class GFG 
{
  
// Function to print the required 
// points which minimizes the sum 
// of Manhattan distances
static void minDistance(int n, int k,
                        int[][] point)
{
      
    // Sorting points in all k dimension 
    for (int i = 0; i < k; i++)
        Array.Sort(point[i]);
      
    // Output the required k points
    for (int i = 0; i < k; i++)
        System.Console.Write(point[i][(int)
            Math.Ceiling((double)(n / 2) - 1)] + " ");
}
  
// Driver code
public static void Main() 
{
    int n = 4;
    int k = 4;
    int[][] point = new int[][]{ new int[]{ 1, 5, 2, 4 }, 
                    new int[]{ 6, 2, 0, 6 }, 
                    new int[]{ 9, 5, 1, 3 }, 
                    new int[]{ 6, 7, 5, 9 } };
      
    // function call to print required points
    minDistance(n, k, point);
}
}
  
// This code is contributed by mits


PHP


输出:
2 2 3 6

时间复杂度: O(k * nlog(n)