# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1147943 | sossa_abel | Coin Collecting (JOI19_ho_t4) | Java | 0 ms | 0 KiB |
import java.io.*;
import java.util.*;
public class Main {
static class State {
int x, y;
int distance;
State(int x, int y, int distance) {
this.x = x;
this.y = y;
this.distance = distance;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<int[]> coins = new ArrayList<>();
for (int i = 0; i < 2 * n; i++) {
String[] parts = br.readLine().split(" ");
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]);
coins.add(new int[]{x, y});
}
long totalMoves = 0;
boolean[] used = new boolean[2 * n];
for (int x = 1; x <= n; x++) {
for (int y = 1; y <= 2; y++) {
int minMoves = Integer.MAX_VALUE;
int bestCoin = -1;
for (int i = 0; i < 2 * n; i++) {
if (!used[i]) {
int moves = Math.abs(coins.get(i)[0] - x) +
Math.abs(coins.get(i)[1] - y);
if (moves < minMoves) {
minMoves = moves;
bestCoin = i;
}
}
}
if (bestCoin != -1) {
used[bestCoin] = true;
totalMoves += minMoves;
}
}
}
System.out.println(totalMoves);
}
}