-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek4_Day3.java
More file actions
51 lines (41 loc) · 1.28 KB
/
Week4_Day3.java
File metadata and controls
51 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package Algorithm4;
import java.util.Scanner;
public class Week4_Day3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 입력값 받기
int K = sc.nextInt();
int M = sc.nextInt();
// 라인의 길이 배열 생성 및 초기화
long[] lines = new long[K];
long max = 0;
for (int i = 0; i < K; i++) {
lines[i] = sc.nextLong();
if (lines[i] > max) {
max = lines[i];
}
}
// 이분 탐색을 위한 변수 초기화
long left = 1;
long right = max;
long result = 0;
// 이분 탐색 실행
while (left <= right) {
long sum = 0;
long mid = (left + right) / 2;
// 라인 나누기 계산
for (int j = 0; j < K; j++) {
sum += (lines[j] / mid);
}
// 충분한 조각 수가 나오지 않을 때
if (sum < M) {
right = mid - 1;
} else { // 충분한 조각 수가 나올 때
result = mid; // 가능한 최대 길이를 저장
left = mid + 1;
}
}
// 결과 출력
System.out.println(result);
}
}