제출 #1148103

#제출 시각아이디문제언어결과실행 시간메모리
1148103humerez_sCoin Collecting (JOI19_ho_t4)Java
0 / 100
84 ms12880 KiB
import java.util.*; public class joi2019_ho_t4 { static int n; static List<Integer> xCoords = new ArrayList<>(); static List<Integer> yCoords = new ArrayList<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); int totalCoins = 2 * n; for (int i = 0; i < totalCoins; i++) { int x = sc.nextInt(); int y = sc.nextInt(); xCoords.add(x); yCoords.add(y); } Collections.sort(xCoords); Collections.sort(yCoords); List<Integer> targetX = new ArrayList<>(); List<Integer> targetY = new ArrayList<>(); for (int i = 1; i <= n; i++) { targetX.add(i); targetX.add(i); } for (int i = 1; i <= n; i++) { targetY.add(1); } for (int i = 1; i <= n; i++) { targetY.add(2); } if (targetX.size() != totalCoins || targetY.size() != totalCoins) { System.err.println("Error: Los tamaños de targetX o targetY no coinciden."); return; } long result = calculateMinimumMoves(targetX, targetY); System.out.println(result); sc.close(); } private static long calculateMinimumMoves(List<Integer> targetX, List<Integer> targetY) { int size = xCoords.size(); long[][] dp = new long[size + 1][size + 1]; for (int i = 0; i <= size; i++) { Arrays.fill(dp[i], Long.MAX_VALUE); } dp[0][0] = 0; for (int i = 1; i <= size; i++) { for (int j = 1; j <= size; j++) { long moveCost = Math.abs(xCoords.get(i - 1) - targetX.get(j - 1)) + Math.abs(yCoords.get(i - 1) - targetY.get(j - 1)); if (dp[i - 1][j - 1] != Long.MAX_VALUE) { dp[i][j] = Math.min(dp[i - 1][j - 1] + moveCost, dp[i - 1][j]); } else { dp[i][j] = dp[i - 1][j]; } } } return dp[size][size]; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...