import java.util.*;
public class joi2019_ho_t4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // Leer N
int[] x = new int[2 * N];
int[] y = new int[2 * N];
// Leer las posiciones de las 2N monedas
for (int i = 0; i < 2 * N; i++) {
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
// Las posiciones objetivo de las monedas están en las filas 1 a N y columnas 1 y 2
int[] targetX = new int[N];
int[] targetY = new int[2];
// Establecemos las posiciones de destino
for (int i = 0; i < N; i++) {
targetX[i] = i + 1; // Las posiciones en X son 1..N
}
targetY[0] = 1; // Primera columna
targetY[1] = 2; // Segunda columna
// Ordenamos las coordenadas de las monedas en X y Y
Arrays.sort(x);
Arrays.sort(y);
// Crear una lista de todas las posiciones objetivo de las monedas
List<int[]> targets = new ArrayList<>();
for (int i = 0; i < N; i++) {
targets.add(new int[]{i + 1, 1});
targets.add(new int[]{i + 1, 2});
}
// Calcular el número mínimo de movimientos
int totalMoves = 0;
int targetIndex = 0;
// Emparejar las monedas con las posiciones objetivo
for (int i = 0; i < 2 * N; i++) {
int[] target = targets.get(targetIndex++);
totalMoves += Math.abs(x[i] - target[0]) + Math.abs(y[i] - target[1]);
}
// Imprimir el resultado
System.out.println(totalMoves);
sc.close();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |