코딩 테스트/백준

[백준1654/JAVA] 랜선 자르기

리져니 2022. 2. 21. 16:55

https://www.acmicpc.net/problem/1654

 

1654번: 랜선 자르기

첫째 줄에는 오영식이 이미 가지고 있는 랜선의 개수 K, 그리고 필요한 랜선의 개수 N이 입력된다. K는 1이상 10,000이하의 정수이고, N은 1이상 1,000,000이하의 정수이다. 그리고 항상 K ≦ N 이다. 그

www.acmicpc.net

 

풀이

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(), " ");
        long k = Integer.parseInt(st.nextToken());
        long n = Integer.parseInt(st.nextToken());

        ArrayList<Integer> arr = new ArrayList<>();
        long end = 0;

        for(int i=0; i < k; i++){
            int input = Integer.parseInt(br.readLine());
            arr.add(input);
            end = Math.max(end, input);
        }

        long start = 1;

        while(start <= end){
            long mid = (start + end) / 2;
            int total = 0;

            for(Integer e: arr) {
                total += e / mid;
            }

            if(total >= n)
                start = mid + 1;
            else
                end = mid - 1;
        }

        System.out.println(end);

    }
}
728x90

'코딩 테스트 > 백준' 카테고리의 다른 글

[백준1929/JAVA] 소수 구하기  (0) 2022.02.25
[백준1874/JAVA] 스택 수열  (0) 2022.02.23
[백준11866/JAVA] 요세푸스 문제 0  (0) 2022.02.14
[백준10866/JAVA] 덱  (0) 2022.01.26
[백준10845/JAVA] 큐  (0) 2022.01.26