import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
int[] result = new int[N];
Stack<Integer> stack = new Stack<>();
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
stack.push(0);
for(int i=1; i<N; i++) {
while (!stack.isEmpty() && arr[i] > arr[stack.peek()]) {
result[stack.pop()] = arr[i];
}
stack.push(i);
}
while(!stack.isEmpty()) {
result[stack.pop()] = -1;
}
StringBuilder sb = new StringBuilder();
for(int num : result) {
sb.append(num + " ");
}
System.out.println(sb);
br.close();
}
}
'Coding Test > 백준' 카테고리의 다른 글
[백준/자바] 2750 - 수 정렬하기 (0) | 2023.06.05 |
---|---|
[백준/자바] 11286 - 절댓값 힙 (0) | 2023.06.04 |
[백준/자바] 1874 - 스택 수열 (0) | 2023.06.04 |
[백준/자바] 11003 - 최솟값 찾기 (0) | 2023.06.04 |
[백준/자바] 12891 - DNA 비밀번호 (0) | 2023.06.03 |