슈콩

[BOJ] 백준 1477 휴게소 세우기 본문

Algorithms/Baekjoon

[BOJ] 백준 1477 휴게소 세우기

shukong 2025. 9. 18. 00:42

[문제]

https://www.acmicpc.net/problem/1477

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Main {
	static int n,m;
	static int[] rest;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		int l = Integer.parseInt(st.nextToken());
		rest = new int[n+2];
		st = new StringTokenizer(br.readLine());
		rest[0] = 0;
		rest[n+1] = l;
		for(int i=1;i<=n;i++) {
			rest[i] = Integer.parseInt(st.nextToken());
		}
		Arrays.sort(rest);
		
		int result = 0;
		int start = 1;
		int end = l;
		while(start<=end) {
			int mid = (start + end) / 2;
			if(canRest(mid)) {
				end = mid - 1;
			}
			else {
				start = mid + 1;
			}
		}
		System.out.println(start);
	}
	private static boolean canRest(int mid) {
		int cnt = 0;
		for(int i=1;i<=n+1;i++) {
			int diff = rest[i] - rest[i-1];
			cnt += (diff-1) / mid;
		}
		return cnt<=m;
	}
}

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 14921 용액 합성하기  (0) 2025.09.18
[BOJ] 백준 13460 구슬 탈출 2  (2) 2025.09.18
[BOJ] 백준 1253 좋다  (0) 2025.09.17
[BOJ] 백준 13335 트럭  (0) 2025.09.17
[BOJ] 백준 12100 2024 Easy  (0) 2025.09.17