백준

백준 17298번: 오큰수

2호0 2023. 2. 3. 20:07

https://www.acmicpc.net/problem/17298

 

17298번: 오큰수

첫째 줄에 수열 A의 크기 N (1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄에 수열 A의 원소 A1, A2, ..., AN (1 ≤ Ai ≤ 1,000,000)이 주어진다.

www.acmicpc.net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import sys
input = sys.stdin.readline
 
= int(input())
= list(map(int, input().split()))
stack = [0]
ans = [-1]*n
 
for i in range(1,n):
    while stack and a[stack[-1]] < a[i]:
        ans[stack.pop()] = a[i]
    stack.append(i)
    
print(*ans)
cs