N = int(input())
arr = list(map(int,input().split()))
arr.reverse()
result = [-1]
stack = [arr[0]]
idx = 1
while idx <N:
while stack and stack[-1] <= arr[idx]:
stack.pop()
if stack:
result.append(stack[-1])
else:
result.append(-1)
stack.append(arr[idx])
idx += 1
result.reverse()
print(*result)
import sys
input = sys.stdin.readline
N = int(input())
arr = list(map(int,input().split()))
result = [-1]*N
stack = [0]
idx = 1
while stack and idx<N:
while stack and arr[stack[-1]] < arr[idx]:
result[stack.pop()] = arr[idx]
stack.append(idx)
idx += 1
print(*result)
'알고리즘 > 백준_복기_미완료' 카테고리의 다른 글
[BOJ/백준] 17406 배열 돌리기 4 (0) | 2021.05.06 |
---|---|
[BOJ/백준] 17404 RGB 거리 2 (0) | 2021.05.06 |
[BOJ/백준] 17281 ⚾ (0) | 2021.05.06 |
[BOJ/백준] 17114 하이퍼 토마토 (0) | 2021.05.06 |
[BOJ/백준] 17136 색종이 붙이기 (0) | 2021.05.06 |