[백준 - JAVA] 스택 - Gold 5 탑 (2493)
import java.io.*;
import java.util.Stack;
import java.util.StringTokenizer;
public class BOJ_2493 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
Stack<int[]> stack = new Stack<>();
int N = Integer.parseInt(in.readLine());
StringTokenizer st = new StringTokenizer(in.readLine());
int i = 1;
while(st.hasMoreTokens()) {
int n = Integer.parseInt(st.nextToken());
while(!stack.isEmpty()) {
if(stack.peek()[1] > n) {
sb.append(stack.peek()[0]).append(" ");
break;
}
stack.pop();
}
if(stack.isEmpty()) {
sb.append(0).append(" ");
}
stack.push(new int[]{i, n});
i++;
}
System.out.println(sb);
}
}