Skip to content

Commit 9a9d27f

Browse files
authored
[20250716] BOJ / G5 / 탑 / 이강현
1 parent da4741e commit 9a9d27f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

lkhyun/202507/16 BOJ G5 탑.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main{
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int N;
10+
static int[] tops;
11+
static int[] reception;
12+
13+
public static void main(String[] args) throws Exception {
14+
N = Integer.parseInt(br.readLine());
15+
st = new StringTokenizer(br.readLine());
16+
tops = new int[N+1];
17+
reception = new int[N+1];
18+
19+
for (int i = 1; i <= N; i++) {
20+
tops[i] = Integer.parseInt(st.nextToken());
21+
}
22+
23+
ArrayDeque<int[]> stack = new ArrayDeque<>();
24+
stack.push(new int[]{tops[N],N});
25+
26+
for (int i = N-1; i >= 1; i--) {
27+
while(!stack.isEmpty() && tops[i] > stack.peek()[0]){
28+
int[] top = stack.pop();
29+
reception[top[1]] = i;
30+
}
31+
stack.push(new int[]{tops[i],i});
32+
}
33+
34+
StringBuilder sb = new StringBuilder();
35+
for (int i = 1; i <= N; i++) {
36+
sb.append(reception[i]);
37+
if(i != N){
38+
sb.append(" ");
39+
}
40+
}
41+
bw.write(sb.toString());
42+
bw.close();
43+
}
44+
}
45+
```

0 commit comments

Comments
 (0)