본문 바로가기
Baekjoon Online Judge

[BOJ] 백준 1654번: 랜선 자르기 (Java)

by 프로그래 밍구 2023. 4. 3.

문제링크

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.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;

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 K = Integer.parseInt(st.nextToken());
        int N = Integer.parseInt(st.nextToken());
        
        int[] arr = new int[K];
        
        long max = 0;
        
        for(int i = 0; i < K; i++){
            arr[i] = Integer.parseInt(br.readLine());
            
            if(max < arr[i]){
                max = arr[i];
            }    
        }
        
        max = max + 1;
        
        long min = 0;
        long mid = 0;
        
        while(min < max){
            long count = 0;
            
            mid = (max + min) / 2;
            
            for(int i = 0; i < K; i++){
                count = count + (arr[i] / mid);
            }
            
            if(count < N){
                max = mid;
            }else{
                min = mid + 1;
            }
        }
        
        System.out.print(min - 1);
    }
}

 

댓글