From 28817df3d1b4a0efab2c4d53358917976c47a3cf Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Sat, 29 Nov 2025 19:46:33 +0900 Subject: [PATCH] =?UTF-8?q?[20251129]=20BOJ=20/=20G5=20/=20=EC=84=A0=20?= =?UTF-8?q?=EA=B8=8B=EA=B8=B0=20/=20=EC=9D=B4=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... \354\204\240 \352\270\213\352\270\260.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "JHLEE325/202511/29 BOJ G5 \354\204\240 \352\270\213\352\270\260.md" diff --git "a/JHLEE325/202511/29 BOJ G5 \354\204\240 \352\270\213\352\270\260.md" "b/JHLEE325/202511/29 BOJ G5 \354\204\240 \352\270\213\352\270\260.md" new file mode 100644 index 00000000..83f76c3e --- /dev/null +++ "b/JHLEE325/202511/29 BOJ G5 \354\204\240 \352\270\213\352\270\260.md" @@ -0,0 +1,64 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static class line { + int x, y; + line(int x, int y) { + this.x = x; + this.y = y; + } + } + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + int N = Integer.parseInt(br.readLine()); + + line[] arr = new line[N]; + + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + if (a > b) { + int temp = a; + a = b; + b = temp; + } + + arr[i] = new line(a, b); + } + + Arrays.sort(arr, (o1, o2) -> { + if (o1.x == o2.x) return o1.y - o2.y; + return o1.x - o2.x; + }); + + long answer = 0; + + int curStart = arr[0].x; + int curEnd = arr[0].y; + + for (int i = 1; i < N; i++) { + int nx = arr[i].x; + int ny = arr[i].y; + + if (nx <= curEnd) { + curEnd = Math.max(curEnd, ny); + } else { + answer += (curEnd - curStart); + curStart = nx; + curEnd = ny; + } + } + + answer += (curEnd - curStart); + + System.out.println(answer); + } +} +```