BOJ 1911 흙길 보수하기
BOJ 1911 흙길 보수하기
https://www.acmicpc.net/problem/1911
문제
어젯밤 겨울 캠프 장소에서 월드 본원까지 이어지는, 흙으로 된 비밀길 위에 폭우가 내려서 N(1 ≤ N ≤ 10,000)개의 물웅덩이가 생겼다. 월드학원은 물웅덩이를 덮을 수 있는 길이가 L(1 ≤ L ≤ 1,000,000)인 널빤지들을 충분히 가지고 있어서, 이들로 다리를 만들어 물웅덩이들을 모두 덮으려고 한다. 물웅덩이들의 위치와 크기에 대한 정보가 주어질 때, 모든 물웅덩이들을 덮기 위해 필요한 널빤지들의 최소 개수를 구하여라.
입력
첫째 줄에 두 정수 N과 L이 들어온다.
둘째 줄부터 N+1번째 줄까지 총 N개의 줄에 각각의 웅덩이들의 정보가 주어진다. 웅덩이의 정보는 웅덩이의 시작 위치와 끝 위치로 이루어진다. 각 위치는 0 이상 1,000,000,000 이하의 정수이다. 입력으로 주어지는 웅덩이는 겹치지 않는다.
출력
첫째 줄에 모든 물웅덩이들을 덮기 위해 필요한 널빤지들의 최소 개수를 출력한다.
예제 입력 1
3 3 1 6 13 17 8 12
예제 출력 1
5
코드
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
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
static int N;
static int L;
static PriorityQueue<Point> puddles = new PriorityQueue<>(Comparator.comparingInt(point -> point.x));
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());
L = Integer.parseInt(st.nextToken());
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
puddles.offer(new Point(start, end));
}
int answer = calculateMinBoard();
System.out.println(answer);
}
static int calculateMinBoard() {
int count = 0;
int lastCovered = -1;
while (!puddles.isEmpty()) {
Point puddle = puddles.poll();
if(lastCovered > puddle.x) {
puddle.x = lastCovered;
}
double puddleWidth = puddle.y - puddle.x;
if(puddleWidth > 0) {
int newBoards = (int) Math.ceil(puddleWidth / L);
count += newBoards;
lastCovered = puddle.x + L * newBoards;
}
}
return count;
}
}
This post is licensed under CC BY 4.0 by the author.