랜선 자르기 1654

LJM·2023년 2월 6일
0

백준풀기

목록 보기
74/259

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

이문제같은경우 int형을 사용하면 어디서 오버플로우날지 몰라서 시간이 걸렸다
그냥 다 long으로 해버렸다;;

import java.io.*;
import java.util.*;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] input = br.readLine().split(" ");

        int k = Integer.parseInt(input[0]);
        int n = Integer.parseInt(input[1]);

        int[] arr = new int[k];

        for(int i = 0; i < k; ++i)
        {
            arr[i] = Integer.parseInt(br.readLine());
        }

        long right = Integer.MAX_VALUE;
        long left = 0;
        long cand = 0;
        long ans = 0;

        while(left <= right)
        {
            cand = (left + right) / 2;

            if(n <= cal(cand, arr))
            {
                ans = cand;
                left = cand + 1;
            }
            else
            {
                right = cand - 1;
            }
        }

        System.out.println(ans);
    }

    public static long cal(long cand, int[] arr)
    {
        if(cand == 0)
            cand = 1;

        long ret = 0;
        for(int element : arr)
        {
            if(element >= cand)
                ret += element / cand;
        }

        return ret;
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글