Submission #1147508

#TimeUsernameProblemLanguageResultExecution timeMemory
1147508leo12345Coin Collecting (JOI19_ho_t4)Java
0 / 100
81 ms12612 KiB
import java.util.*;

public class joi2019_ho_t4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int[][] coins = new int[2 * N][2];

        for (int i = 0; i < 2 * N; i++) {
            coins[i][0] = scanner.nextInt();
            coins[i][1] = scanner.nextInt();
        }

        List<int[]> targets = new ArrayList<>();
        for (int x = 1; x <= N; x++) {
            for (int y = 1; y <= 2; y++) {
                targets.add(new int[]{x, y});
            }
        }

        Arrays.sort(coins, (a, b) -> a[0] == b[0] ? Integer.compare(a[1], b[1]) : Integer.compare(a[0], b[0]));
        targets.sort((a, b) -> a[0] == b[0] ? Integer.compare(a[1], b[1]) : Integer.compare(a[0], b[0]));

        int 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]);
        }

        System.out.println(totalMoves);
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...