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});
}
List<int[]> targets = new ArrayList<>();
for (int x = 1; x <= N; x++) {
targets.add(new int[]{x, 1});
targets.add(new int[]{x, 2});
}
coins.sort((a, b) -> Integer.compare(a[0], b[0]));
targets.sort((a, b) -> Integer.compare(a[0], b[0]));
PriorityQueue<long[]> pq = new PriorityQueue<>((a, b) -> Long.compare(a[0], b[0]));
for (int i = 0; i < 2 * N; i++) {
int[] coin = coins.get(i);
for (int j = 0; j < 2 * N; j++) {
int[] target = targets.get(j);
long distance = (long) Math.abs(coin[0] - target[0]) + (long) Math.abs(coin[1] - target[1]);
pq.offer(new long[]{distance, i, j});
}
}
boolean[] assignedCoins = new boolean[2 * N];
boolean[] assignedTargets = new boolean[2 * N];
long totalOps = 0;
while (!pq.isEmpty()) {
long[] entry = pq.poll();
long distance = entry[0];
int coinIndex = (int) entry[1];
int targetIndex = (int) entry[2];
if (!assignedCoins[coinIndex] && !assignedTargets[targetIndex]) {
assignedCoins[coinIndex] = true;
assignedTargets[targetIndex] = true;
totalOps += distance;
}
}
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... |