Skip to content

Commit 99a1e05

Browse files
committed
고다혜: [BOJ] 2170 선 긋기_250207
1 parent b61c5b3 commit 99a1e05

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

BOJ/1000-5000번/DH_2170.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 선 긋기
6+
*/
7+
8+
public class DH_2170 {
9+
static class Point implements Comparable<Point> {
10+
int s, e;
11+
public Point(int s, int e) {
12+
this.s = s;
13+
this.e = e;
14+
}
15+
16+
@Override
17+
public int compareTo(Point o) {
18+
if(this.s != o.s) return Integer.compare(this.s, o.s);
19+
return Integer.compare(this.e, o.e);
20+
}
21+
}
22+
23+
static final int MIN = -1_000_000_000, MAX = 1_000_000_000;
24+
public static void main(String[] args) throws Exception {
25+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
26+
StringTokenizer st;
27+
28+
int N = Integer.parseInt(br.readLine());
29+
30+
PriorityQueue<Point> pq = new PriorityQueue<Point>();
31+
32+
// 우선순위큐에 점들 넣어주기
33+
for(int i = 0; i < N; i++) {
34+
st = new StringTokenizer(br.readLine());
35+
36+
int s = Integer.parseInt(st.nextToken());
37+
int e = Integer.parseInt(st.nextToken());
38+
39+
pq.add(new Point(s, e));
40+
}
41+
42+
// (MAX, MAX) 로 넣으면 마지막에 넣은 점이 MAX일 때, 올바른 정답이 안나옴
43+
pq.add(new Point(MAX + 1, MAX + 1));
44+
45+
int start = MIN, end = MIN;
46+
int result = 0;
47+
48+
while(!pq.isEmpty()) {
49+
Point current = pq.poll();
50+
51+
// 현재 시작점이 이전까지 확인한 지점들의 끝점보다 크면
52+
// 이전 점들을 이어서 만들어진 선분의 길이 더해주기
53+
if(end < current.s) {
54+
result += end - start;
55+
56+
start = current.s;
57+
end = current.e;
58+
59+
} else {
60+
end = Math.max(end, current.e);
61+
}
62+
}
63+
64+
System.out.println(result);
65+
}
66+
}

0 commit comments

Comments
 (0)