Submission #1148145

#TimeUsernameProblemLanguageResultExecution timeMemory
1148145ruben_ipenzaCoin Collecting (JOI19_ho_t4)Java
0 / 100
76 ms12612 KiB
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];

        // Read the positions of the coins
        for (int i = 0; i < 2 * N; i++) {
            coins[i][0] = sc.nextInt();
            coins[i][1] = sc.nextInt();
        }

        // Generate target positions
        int[][] targets = new int[2 * N][2];
        for (int i = 0; i < N; i++) {
            targets[2 * i][0] = i + 1;
            targets[2 * i][1] = 1;
            targets[2 * i + 1][0] = i + 1;
            targets[2 * i + 1][1] = 2;
        }

        // Sort coins and targets
        Arrays.sort(coins, (a, b) -> {
            if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
            return Integer.compare(a[1], b[1]);
        });

        Arrays.sort(targets, (a, b) -> {
            if (a[0] != b[0]) return Integer.compare(a[0], b[0]);
            return Integer.compare(a[1], b[1]);
        });

        // Calculate the total number of moves
        long totalMoves = 0;
        for (int i = 0; i < 2 * N; i++) {
            totalMoves += Math.abs(coins[i][0] - targets[i][0]) + Math.abs(coins[i][1] - targets[i][1]);
        }

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