알고리즘/이분탐색

[백준] 1654번 : 랜선 자르기 #Java

VIPeveloper 2022. 5. 8. 11:41
728x90
반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {


    public static void main(String[] args) throws IOException {
        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 [] arr = new int [n];
        long max_len = Integer.MIN_VALUE;

        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(br.readLine());
            max_len = Math.max(max_len,arr[i]);
        }

        // 예외처리
        max_len++;

        long lo=0,mid;
        
        // Upper Bound
        while (lo < max_len){

            mid = (lo+max_len) / 2;

            long count = 0;

            for (int i = 0; i < arr.length; i++) {
                count += (arr[i] / mid);
            }

            if(count < m){
                max_len = mid;
            }else{
                lo = mid+1;
            }
        }
        System.out.println((lo - 1));
    }
}
728x90
반응형

'알고리즘 > 이분탐색' 카테고리의 다른 글

[백준] 2805번 나무 자르기 #Java  (0) 2022.05.09
[백준] 10816 숫자카드 2 #Java  (0) 2022.05.07