Dados N puntos en K espacio dimensional donde y . La tarea es determinar el punto tal que la suma de las distancias de Manhattan desde este punto hasta los N puntos se minimice. La distancia de Manhattan es la distancia entre dos puntos medida a lo largo de ejes en ángulo recto. En un plano con p1 en (x1, y1) y p2 en (x2, y2) , es |x1 – x2| + |y1 – y2| .
Ejemplos:
Entrada: N = 3, K = 3, Puntos = {1, 1, 1}, {2, 2, 2}, {3, 3, 3} Salida: 2 2 2 Entrada: N = 4,
K =
4 , Puntos = {1, 6, 9, 6}, {5, 2, 5, 7}, {2, 0, 1, 5}, {4, 6, 3, 9} Resultado: 2 2
3 6
Enfoque: Para minimizar la distancia de Manhattan, todo lo que tenemos que hacer es ordenar los puntos en todas las K dimensiones y generar los elementos intermedios de cada una de las K dimensiones.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to print the required points which // minimizes the sum of Manhattan distances void minDistance(int n, int k, vector<vector<int> >& 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<vector<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); 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
<?php // PHP implementation of above approach // Function to print the required // points which minimizes the sum // of Manhattan distances function minDistance($n, $k, &$point) { // Sorting points in all // k dimension for ($i = 0; $i < $k; ++$i) sort($point[$i]); // Output the required k points for ($i = 0; $i < $k; ++$i) echo $point[$i][(ceil( (double)$n / 2) - 1)] . " "; } // Driver code $n = 4; $k = 4; $point = array(array( 1, 5, 2, 4 ), array( 6, 2, 0, 6 ), array( 9, 5, 1, 3 ), array( 6, 7, 5, 9 )); // function call to print // required points minDistance($n, $k, $point); // This code is contributed // by ChitraNayal ?>
Javascript
<script> // Javascript implementation of above approach // Function to print the required // points which minimizes the sum // of Manhattan distances function minDistance(n,k,points) { // Sorting points in all k dimension for (let i = 0; i < k; i++) (point[i]).sort(function(a,b){return a-b;}); // Output the required k points for (let i = 0; i < k; i++) document.write(point[i][ Math.ceil((n / 2) - 1)] + " "); } // Driver code let n = 4; let k = 4; let 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 rag2127 </script>
2 2 3 6
Complejidad del tiempo: O(k*nlog(n)
Publicación traducida automáticamente
Artículo escrito por Sanjit_Prasad y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA