From 9a9d27f338d5f84d463fc600979faaf17f30ae94 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Wed, 16 Jul 2025 20:26:02 +0900 Subject: [PATCH] =?UTF-8?q?[20250716]=20BOJ=20/=20G5=20/=20=ED=83=91=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 --- "lkhyun/202507/16 BOJ G5 \355\203\221.md" | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "lkhyun/202507/16 BOJ G5 \355\203\221.md" diff --git "a/lkhyun/202507/16 BOJ G5 \355\203\221.md" "b/lkhyun/202507/16 BOJ G5 \355\203\221.md" new file mode 100644 index 00000000..d91be164 --- /dev/null +++ "b/lkhyun/202507/16 BOJ G5 \355\203\221.md" @@ -0,0 +1,45 @@ +```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 StringTokenizer st; + static int N; + static int[] tops; + static int[] reception; + + public static void main(String[] args) throws Exception { + N = Integer.parseInt(br.readLine()); + st = new StringTokenizer(br.readLine()); + tops = new int[N+1]; + reception = new int[N+1]; + + for (int i = 1; i <= N; i++) { + tops[i] = Integer.parseInt(st.nextToken()); + } + + ArrayDeque stack = new ArrayDeque<>(); + stack.push(new int[]{tops[N],N}); + + for (int i = N-1; i >= 1; i--) { + while(!stack.isEmpty() && tops[i] > stack.peek()[0]){ + int[] top = stack.pop(); + reception[top[1]] = i; + } + stack.push(new int[]{tops[i],i}); + } + + StringBuilder sb = new StringBuilder(); + for (int i = 1; i <= N; i++) { + sb.append(reception[i]); + if(i != N){ + sb.append(" "); + } + } + bw.write(sb.toString()); + bw.close(); + } +} +```