https://www.acmicpc.net/problem/1874
1874번: 스택 수열
1부터 n까지에 수에 대해 차례로 [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] 연산을 수행하면 수열 [4, 3, 6, 8, 7, 5, 2, 1]을 얻을 수 있다.
www.acmicpc.net
import sys
input = sys.stdin.readline
n = int(input())
stack = []
result = []
possible = True
push_num = 1
for i in range(n):
num = int(input())
while num >= push_num:
stack.append(push_num)
result.append('+')
push_num += 1
if stack[-1] == num:
stack.pop()
result.append('-')
else:
possible = False
if possible:
for j in result:
print(j)
else:
print('NO')
'알고리즘 > 백준' 카테고리의 다른 글
[백준 1260][파이썬] DFS와 BFS (0) | 2022.02.24 |
---|---|
[백준 11725][파이썬] 트리의 부모 찾기 (0) | 2022.02.23 |
[백준 1038][파이썬] 감소하는 수 (0) | 2022.02.21 |
[백준 7568][파이썬] 덩치 (0) | 2022.02.18 |
[백준 1182][파이썬] 부분수열의 합 (0) | 2022.02.17 |