File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed
Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.BufferedReader ;
3+ import java.io.IOException ;
4+ import java.io.InputStreamReader ;
5+ import java.util.Arrays ;
6+
7+ public class B1477 {
8+ private static BufferedReader br;
9+ private static int n;
10+ private static int l;
11+ private static int [] positions;
12+ private static int m;
13+
14+ public static void main (String [] args ) throws IOException {
15+ br = new BufferedReader (new InputStreamReader (System . in));
16+ String [] temp = br. readLine(). split(" " );
17+ n = Integer . parseInt(temp[0 ]);
18+ m = Integer . parseInt(temp[1 ]);
19+ l = Integer . parseInt(temp[2 ]);
20+
21+ positions = new int [n + 2 ];
22+ String [] line = br. readLine(). split(" " );
23+ for (int i = 0 ; i < n; i++ ) {
24+ positions[i + 1 ] = Integer . parseInt(line[i]);
25+ }
26+ positions[0 ] = 0 ;
27+ positions[n + 1 ] = l;
28+
29+ Arrays . sort(positions);
30+
31+ int left = 1 ;
32+ int right = l;
33+ int answer = 0 ;
34+
35+ while (left <= right) {
36+ int mid = (left + right) / 2 ;
37+ if (canBuild(mid)) {
38+ answer = mid;
39+ right = mid - 1 ;
40+ } else {
41+ left = mid + 1 ;
42+ }
43+ }
44+
45+ System . out. println(answer);
46+ }
47+
48+ private static boolean canBuild (int maxDist ) {
49+ int needed = 0 ;
50+ for (int i = 1 ; i < positions. length; i++ ) {
51+ int dist = positions[i] - positions[i - 1 ];
52+ // 해당 구간에서 몇 개의 휴게소가 필요한가?
53+ if (dist > maxDist) {
54+ needed += (dist - 1 ) / maxDist;
55+ }
56+ }
57+ return needed <= m;
58+ }
59+ }
60+
61+ ```
You can’t perform that action at this time.
0 commit comments