File tree Expand file tree Collapse file tree 1 file changed +67
-0
lines changed
weekly/week03/BOJ_1477_휴게소세우기 Expand file tree Collapse file tree 1 file changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ package stduy .week03 ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStreamReader ;
6+ import java .util .Arrays ;
7+
8+ // 휴게소 세우기
9+ public class BOJ_1477 {
10+
11+ static int N ;
12+ static int M ;
13+ static int len ;
14+
15+ static int [] rests ;
16+ public static void main (String [] args ) throws IOException {
17+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
18+
19+ String []s = br .readLine ().split (" " );
20+
21+ N = Integer .parseInt (s [0 ]);
22+ M = Integer .parseInt (s [1 ]);
23+ len = Integer .parseInt (s [2 ]);
24+
25+ s = br .readLine ().split (" " );
26+ rests = new int [N +2 ];
27+ rests [0 ] = len ;
28+ for (int i =1 ; i <=N ; i ++){
29+ rests [i ] = Integer .parseInt (s [i -1 ]);
30+ }
31+
32+ Arrays .sort (rests );
33+
34+ int best = findBest ();
35+ System .out .println (best );
36+ }
37+
38+ private static int findBest () {
39+ int st = 1 ;
40+ int en = len -1 ;
41+
42+ int mid = 0 ;
43+ int ret = 0 ;
44+ while (st <= en ){
45+ mid = (st +en )/2 ;
46+ if (check (mid )){
47+ st = mid +1 ;
48+ }else {
49+ ret = mid ;
50+ en = mid -1 ;
51+ }
52+ }
53+ return ret ;
54+ }
55+
56+ private static boolean check (int mid ) {
57+ int cnt = 0 ;
58+ for (int i =1 ; i <rests .length ; i ++){
59+ int dist = rests [i ] - rests [i -1 ];
60+ cnt += dist /mid ;
61+ if (dist % mid == 0 ){
62+ cnt --;
63+ }
64+ }
65+ return cnt > M ;
66+ }
67+ }
You can’t perform that action at this time.
0 commit comments