import java.util.*;
public class joi2019_ho_t4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
List<int[]> coins = new ArrayList<>();
for (int i = 0; i < 2 * N; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
coins.add(new int[]{x, y});
}
// Generate target positions
List<int[]> targets = new ArrayList<>();
for (int x = 1; x <= N; x++) {
targets.add(new int[]{x, 1});
targets.add(new int[]{x, 2});
}
// Sort coins and targets based on x-coordinate
coins.sort((a, b) -> Integer.compare(a[0], b[0]));
targets.sort((a, b) -> Integer.compare(a[0], b[0]));
// Calculate minimum operations using optimal assignment
long totalOps = 0;
for (int i = 0; i < 2 * N; i++) {
int[] coin = coins.get(i);
int[] target = targets.get(i);
totalOps += Math.abs((long) coin[0] - target[0]) + Math.abs((long) coin[1] - target[1]);
}
System.out.println(totalOps);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |