From c565c74934eed595a7ded390e656b7768d2f3ed2 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Fri, 18 Jul 2025 14:02:17 +0900 Subject: [PATCH] =?UTF-8?q?[20250718]=20BOJ=20/=20G5=20/=20=EC=98=A5?= =?UTF-8?q?=EC=83=81=20=EC=A0=95=EC=9B=90=20=EA=BE=B8=EB=AF=B8=EA=B8=B0=20?= =?UTF-8?q?/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0 \352\276\270\353\257\270\352\270\260.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 "lkhyun/202507/18 BOJ G5 \354\230\245\354\203\201 \354\240\225\354\233\220 \352\276\270\353\257\270\352\270\260.md" diff --git "a/lkhyun/202507/18 BOJ G5 \354\230\245\354\203\201 \354\240\225\354\233\220 \352\276\270\353\257\270\352\270\260.md" "b/lkhyun/202507/18 BOJ G5 \354\230\245\354\203\201 \354\240\225\354\233\220 \352\276\270\353\257\270\352\270\260.md" new file mode 100644 index 00000000..1e6819ba --- /dev/null +++ "b/lkhyun/202507/18 BOJ G5 \354\230\245\354\203\201 \354\240\225\354\233\220 \352\276\270\353\257\270\352\270\260.md" @@ -0,0 +1,38 @@ +```java +import java.util.*; +import java.io.*; + +public class Main{ + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static int N; + static int[] buildings; + static long ans = 0; + public static void main(String[] args) throws Exception { + N = Integer.parseInt(br.readLine()); + buildings = new int[N]; + + for (int i = 0; i < N; i++) { + buildings[i] = Integer.parseInt(br.readLine()); + } + + ArrayDeque key = new ArrayDeque<>(); + ArrayDeque value = new ArrayDeque<>(); + key.push(0); + value.push(buildings[0]); + for (int i = 1; i < N; i++) { + while(!key.isEmpty() && buildings[i] >= value.peek()){ + value.pop(); + ans += i - key.pop() - 1; + } + key.push(i); + value.push(buildings[i]); + } + while(!key.isEmpty()){ + ans += N - key.pop() - 1; + } + bw.write(ans+""); + bw.close(); + } +} +```