[BOJ] 2493. 탑
15 Apr 2026
Reading time ~1 minute
풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Stack<Top> stack = new Stack<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++) {
Top top = new Top(i+1, Integer.parseInt(st.nextToken()));
while(!stack.isEmpty() && stack.peek().height < top.height) {
stack.pop();
}
if(stack.isEmpty()) {
System.out.print("0 ");
stack.push(top);
} else {
System.out.print(stack.peek().index+" ");
stack.push(top);
}
}
}
public static class Top {
int index, height;
Top(int index, int height) {
this.index = index;
this.height = height;
}
}
}