From 233193aedbb5417020f71bf650a3c4fe0c0cceb2 Mon Sep 17 00:00:00 2001 From: Ukj0ng <90972240+Ukj0ng@users.noreply.github.com> Date: Mon, 20 Oct 2025 11:14:08 +0900 Subject: [PATCH] =?UTF-8?q?[20251020]=20BOJ=20/=20G5=20/=20=EC=84=A0=20?= =?UTF-8?q?=EA=B8=8B=EA=B8=B0=20/=20=ED=95=9C=EC=A2=85=EC=9A=B1?= 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" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "Ukj0ng/202510/20 BOJ G5 \354\204\240 \352\270\213\352\270\260.md" diff --git "a/Ukj0ng/202510/20 BOJ G5 \354\204\240 \352\270\213\352\270\260.md" "b/Ukj0ng/202510/20 BOJ G5 \354\204\240 \352\270\213\352\270\260.md" new file mode 100644 index 00000000..ef5bb084 --- /dev/null +++ "b/Ukj0ng/202510/20 BOJ G5 \354\204\240 \352\270\213\352\270\260.md" @@ -0,0 +1,55 @@ +``` +import java.io.*; +import java.util.*; + +public class Main { + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static int[][] lines; + private static int N; + public static void main(String[] args) throws IOException { + init(); + int answer = sweep(); + + bw.write(answer + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + + lines = new int[N][2]; + + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + lines[i][0] = Integer.parseInt(st.nextToken()); + lines[i][1] = Integer.parseInt(st.nextToken()); + } + + Arrays.sort(lines, (o1, o2) -> { + if (o1[0] == o2[0]) return Integer.compare(o1[1], o2[1]); + return Integer.compare(o1[0], o2[0]); + }); + } + + private static int sweep() { + int result = 0; + int prevS = -1000000001; + int prevE = -1000000001; + + for (int[] line : lines) { + if (prevE < line[0]) { + result += (prevE - prevS); + prevS = line[0]; + } + + prevE = Math.max(prevE, line[1]); + } + + result += (prevE - prevS); + return result; + } +} +```