Skip to content

Instantly share code, notes, and snippets.

@youngvctr
Created July 24, 2023 14:05
Show Gist options
  • Save youngvctr/7a2b5630f08121b4d1cfacb6f18ec292 to your computer and use it in GitHub Desktop.
Save youngvctr/7a2b5630f08121b4d1cfacb6f18ec292 to your computer and use it in GitHub Desktop.
BOJ | BinarySearch[1654] > 랜선 자르기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
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];
for (int i = 0; i < K; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(arr);
long min = 1;
long mid = 0;
long max = arr[arr.length-1];
while (min <= max) {
mid = min - (min - max) / 2;
long count = 0;
for (int i = 0; i < arr.length; i++) {
count += (arr[i] / mid);
}
if (count >= N) {
min = mid + 1;
} else {
max = mid - 1;
}
}
System.out.println(min - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment