제출 #1148130

#제출 시각아이디문제언어결과실행 시간메모리
1148130pereira_oliverCoin Collecting (JOI19_ho_t4)Java
0 / 100
74 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});
    }

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

    // Sort coins and targets based on x-coordinate
    coins.sort((a, b) -> Integer.compare(a[0], b[0]));
    targets.sort((a, b) -> Integer.compare(a[0], b[0]));

    // Calculate minimum operations using optimal assignment
    long totalOps = 0;
    for (int i = 0; i < 2 * N; i++) {
      int[] coin = coins.get(i);
      int[] target = targets.get(i);
      totalOps += Math.abs((long) coin[0] - target[0]) + Math.abs((long) coin[1] - target[1]);
    }

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