import java.util.*;
public class joi2019_ho_t4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] coins = new int[2 * N][2];
for (int i = 0; i < 2 * N; i++) {
coins[i][0] = sc.nextInt(); // x-coordinate
coins[i][1] = sc.nextInt(); // y-coordinate
}
// List to hold the target positions
List<int[]> targets = new ArrayList<>();
// Generate the target positions
for (int i = 1; i <= N; i++) {
targets.add(new int[]{i, 1});
targets.add(new int[]{i, 2});
}
// Sort the coins and the targets by x and y values
Arrays.sort(coins, Comparator.comparingInt(a -> a[0])); // Sort coins by x-coordinate
targets.sort(Comparator.comparingInt(a -> a[0])); // Sort targets by x-coordinate
// Calculate the minimum number of moves
long totalMoves = 0;
for (int i = 0; i < 2 * N; i++) {
totalMoves += Math.abs(coins[i][0] - targets.get(i)[0]) + Math.abs(coins[i][1] - targets.get(i)[1]);
}
// Output the result
System.out.println(totalMoves);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |