From 96a34b6a02b42edf042e3714d04dab2f39a5f989 Mon Sep 17 00:00:00 2001 From: mong3125 Date: Thu, 28 Mar 2024 23:36:49 +0900 Subject: [PATCH] =?UTF-8?q?BOJ1243=5F=EC=9D=B4=EC=A7=84=ED=83=90=EC=83=89?= =?UTF-8?q?=20java=20solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\355\203\200\353\240\210\354\212\250.java" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 "mong3125/\354\235\264\354\247\204\355\203\220\354\203\211/BOJ2343_\352\270\260\355\203\200\353\240\210\354\212\250.java" diff --git "a/mong3125/\354\235\264\354\247\204\355\203\220\354\203\211/BOJ2343_\352\270\260\355\203\200\353\240\210\354\212\250.java" "b/mong3125/\354\235\264\354\247\204\355\203\220\354\203\211/BOJ2343_\352\270\260\355\203\200\353\240\210\354\212\250.java" new file mode 100644 index 0000000..6f357e2 --- /dev/null +++ "b/mong3125/\354\235\264\354\247\204\355\203\220\354\203\211/BOJ2343_\352\270\260\355\203\200\353\240\210\354\212\250.java" @@ -0,0 +1,66 @@ +package 이진탐색; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.IOException; +import java.util.StringTokenizer; + +public class BOJ2343_기타레슨 { + + static int N; + static int M; + static int[] lectures; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + lectures = new int[N]; + st = new StringTokenizer(br.readLine()); + + int sum = 0; + int max = 0; + for(int i = 0; i < N; i++) { + lectures[i] = Integer.parseInt(st.nextToken()); + sum += lectures[i]; + max = Math.max(max, lectures[i]); + } + + // 블루레이의 크기는 max부터 sum 사이 일것이다. + int result = minimumBlueray(max, sum); + + System.out.println(result); + } + + public static int minimumBlueray(int start, int end) { + if (start < end) return -1; + + int mid = (start + end) / 2; + + if (can(mid)) { + int more = minimumBlueray(start, mid - 1); + return more != -1 ? more : mid; + } + else return minimumBlueray(mid + 1, end); + } + + public static boolean can(int blueray) { + int count = M; + int i = 0; + int stack = blueray; + while (count > 0) { + while (stack - lectures[i] > 0) { + if (i == N) return true; + stack -= lectures[i]; + i++; + } + + stack = blueray; + count--; + } + + return false; + } +}