제출 #1148143

#제출 시각아이디문제언어결과실행 시간메모리
1148143pereira_oliverCoin Collecting (JOI19_ho_t4)Java
0 / 100
87 ms12868 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();
    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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...