Skip to content

Commit fc3fa56

Browse files
authored
[20251024] BOJ / G1 / 통나무 자르기 / 김수연
[20251024] BOJ / G1 / 통나무 자르기 / 김수연
2 parents 530811b + 59d03dd commit fc3fa56

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class boj1114 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
static int L, K, C;
12+
static int[] pos;
13+
static int ansLen, ansPos = -1, firstCut;
14+
public static void main(String[] args) throws Exception {
15+
nextLine();
16+
L = nextInt(); // 통나무 길이
17+
K = nextInt(); // 위치 개수
18+
C = nextInt(); // 자를 수 있는 최대 횟수
19+
// 통나무의 가장 긴 조각을 작게 만들고, 그 길이 구하기
20+
nextLine();
21+
pos = new int[K+2];
22+
pos[0] = 0;
23+
pos[K+1] = L;
24+
for (int i = 1; i <= K; i++) pos[i] = nextInt();
25+
Arrays.sort(pos);
26+
27+
// 가장 긴 조각의 길이, 그 때 처음 자르는 위치
28+
int start = 1, end = L;
29+
ansLen = L;
30+
while (start <= end) {
31+
int mid = (start + end) / 2;
32+
int cnt = calc(mid);
33+
34+
if (cnt <= C) {
35+
ansLen = mid;
36+
ansPos = firstCut;
37+
end = mid - 1;
38+
} else {
39+
start = mid + 1;
40+
}
41+
}
42+
System.out.println(ansLen + " " + ansPos);
43+
}
44+
45+
static int calc(int mid) {
46+
int dist = 0, cnt = 0;
47+
for (int i = K; i >= 0; i--) {
48+
dist += pos[i+1] - pos[i];
49+
if (dist > mid) {
50+
dist = (pos[i+1] - pos[i]);
51+
cnt++;
52+
if (dist > mid) {
53+
cnt = C+1;
54+
break;
55+
}
56+
}
57+
}
58+
if (cnt < C) firstCut = pos[1];
59+
else firstCut = dist;
60+
61+
return cnt;
62+
}
63+
}
64+
```

0 commit comments

Comments
 (0)