From 973487b7c73051a9eef50b5cd487e258f2e4908c Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Fri, 14 Feb 2025 17:21:12 +0900 Subject: [PATCH] =?UTF-8?q?[20250214]=20BOJ=20/=20G2=20/=20=EC=BB=A4?= =?UTF-8?q?=ED=94=8C=20=EB=A7=8C=EB=93=A4=EA=B8=B0=20/=20=EA=B9=80?= =?UTF-8?q?=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \353\247\214\353\223\244\352\270\260.md" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "suyeun84/202502/14 BOJ G2 \354\273\244\355\224\214 \353\247\214\353\223\244\352\270\260.md" diff --git "a/suyeun84/202502/14 BOJ G2 \354\273\244\355\224\214 \353\247\214\353\223\244\352\270\260.md" "b/suyeun84/202502/14 BOJ G2 \354\273\244\355\224\214 \353\247\214\353\223\244\352\270\260.md" new file mode 100644 index 00000000..f7cddb82 --- /dev/null +++ "b/suyeun84/202502/14 BOJ G2 \354\273\244\355\224\214 \353\247\214\353\223\244\352\270\260.md" @@ -0,0 +1,43 @@ +```java +import java.util.*; +import java.io.*; + +class Solution +{ + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + + int[] boy = new int[N]; + int[] girl = new int[M]; + int[][] dp = new int[N+1][M+1]; + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + boy[i] = Integer.parseInt(st.nextToken()); + } + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < M; i++) { + girl[i] = Integer.parseInt(st.nextToken()); + } + Arrays.sort(boy); + Arrays.sort(girl); + + for (int i = 1; i <= N; i++) { + for (int j = 1; j <= M; j++) { + if(i == j) { + dp[i][j] = dp[i-1][j-1] + Math.abs(boy[i-1]-girl[j-1]); + }else if(i > j) { + dp[i][j] = Math.min(dp[i-1][j-1] + Math.abs(boy[i-1]-girl[j-1]), dp[i-1][j]); + }else if(i < j) { + dp[i][j] = Math.min(dp[i-1][j-1] + Math.abs(boy[i-1]-girl[j-1]), dp[i][j-1]); + } + } + } + System.out.println(dp[N][M]); + } +} + +```