Skip to content

Commit 557fa83

Browse files
committed
이지영: [BOJ] 2170 선 긋기_250208
1 parent 8270dd6 commit 557fa83

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

BOJ/1000-5000번/JY_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+
public class JY_2170 {
4+
5+
static class Pos implements Comparable<Pos> {
6+
int x, y;
7+
8+
public Pos(int x, int y) {
9+
super();
10+
this.x = x;
11+
this.y = y;
12+
}
13+
@Override
14+
public int compareTo(Pos other) {
15+
return this.x - other.x;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return "Pos [x=" + x + ", y=" + y + "]";
21+
}
22+
23+
}
24+
25+
public static void main(String[] args) throws IOException {
26+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
27+
StringTokenizer st = new StringTokenizer(br.readLine());
28+
29+
int N = Integer.parseInt(st.nextToken());
30+
31+
List<Pos> pList = new ArrayList<>();
32+
for(int i=0; i<N; i++) {
33+
st = new StringTokenizer(br.readLine());
34+
int x = Integer.parseInt(st.nextToken());
35+
int y = Integer.parseInt(st.nextToken());
36+
37+
pList.add(new Pos(x, y));
38+
}
39+
40+
// x기준으로 정렬
41+
Collections.sort(pList);
42+
43+
int s = pList.get(0).x;
44+
int e = pList.get(0).y;
45+
int ans = 0;
46+
for(int i=1; i<N; i++) {
47+
Pos now = pList.get(i);
48+
49+
// now.x가 end보다 작다면 겹침 -> end 갱신
50+
if(e >= now.x) {
51+
e = Math.max(e, now.y);
52+
}
53+
// 겹치지 않는 경우
54+
else {
55+
// 기존것 더해주고 새로 업데이트
56+
ans += (e - s);
57+
s = now.x;
58+
e = now.y;
59+
}
60+
}
61+
ans += (e - s);
62+
System.out.println(ans);
63+
64+
}
65+
66+
}

0 commit comments

Comments
 (0)