이분 탐색 알고리즘을 사용해야 하며, 만들 수 있는 랜선의 최대 길이를 구해야 하기 때문에 Upper Bound(상한) 방식을 사용해야 한다.
public class BOJ_1654_이분탐색 {
static int[] cables;
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());
long max = 0;
cables = new int[K];
for (int i = 0; i < K; i++) {
cables[i] = Integer.parseInt(br.readLine());
if (cables[i] > max) {
max = cables[i];
}
}
max++;
long min = 0;
long mid = 0;
while (min < max) {
mid = (min + max) / 2;
long count = 0;
for (int cable : cables) {
count += cable / mid;
}
if (count < N) {
max = mid;
} else {
min = mid + 1;
}
}
System.out.println(min - 1);
}
}