2493-g5 성공

This commit is contained in:
2026-03-09 09:42:10 +09:00
parent 45d79b7bd0
commit 4d7b40c0fd

View File

@@ -0,0 +1,33 @@
# 탑
import sys
input = sys.stdin.readline
def solution():
n = int(input().rstrip())
tops = list(map(int, input().rstrip().split()))
s = []
result = [0] * n
for i in range(n-1, -1, -1):
while s and tops[i] >= s[-1][1]:
idx, _ = s.pop()
result[idx] = i+1
s.append((i, tops[i]))
print(*result)
return
solution()
"""
걸린 시간: 17분
시간 복잡도: 한 탑당 한번씩 stack에 들어가고 나오기 때문에 O(n)이다.
해설: 왼쪽부터 확인하면서 stack에 넣고, stack 있는 것들이 현재 보는 것보다 작은 것은 다 빼면
그게 stack 있는 애들한테 처음으로 만나는 가장 크거나 같은 탑이다. 그리고 그 탑이 신호를 수신한다는 뜻이다.
stack에 남아있는 애들은 본인보다 큰 것을 보지 못한 애들이기 때문에 기존 초기화에서 했던 0으로 냅두면 된다.
"""