Skip to content

Commit 1aee4a8

Browse files
authored
Merge pull request #1318 from AlgorithmWithGod/JHLEE325
[20251105] BOJ / G5 / 아우으 우아으이야!! / 이준희
2 parents 046f883 + a773ba2 commit 1aee4a8

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
static class line implements Comparable<line> {
8+
int x, y;
9+
line(int x, int y) { this.x = x; this.y = y; }
10+
@Override
11+
public int compareTo(line o) {
12+
if (this.x != o.x) return Integer.compare(this.x, o.x);
13+
return Integer.compare(this.y, o.y);
14+
}
15+
}
16+
17+
public static void main(String[] args) throws IOException {
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
20+
int N = Integer.parseInt(br.readLine());
21+
22+
line[] arr = new line[N];
23+
for (int i = 0; i < N; i++) {
24+
StringTokenizer st = new StringTokenizer(br.readLine());
25+
int a = Integer.parseInt(st.nextToken());
26+
int b = Integer.parseInt(st.nextToken());
27+
arr[i] = new line(a, b);
28+
}
29+
Arrays.sort(arr);
30+
31+
long res = 0;
32+
int x = arr[0].x;
33+
int y = arr[0].y;
34+
35+
for (int i = 1; i < N; i++) {
36+
int ns = arr[i].x;
37+
int ne = arr[i].y;
38+
if (ns <= y) {
39+
if (ne > y) {
40+
y = ne;
41+
}
42+
} else {
43+
res += (long)(y - x);
44+
x = ns;
45+
y = ne;
46+
}
47+
}
48+
res += (long)(y - x);
49+
50+
System.out.println(res);
51+
}
52+
}
53+
```

0 commit comments

Comments
 (0)