https://www.acmicpc.net/problem/1874
1, 2, 3, ..., n까지의 숫자를 차례로 적당히 push/pop 하면서
주어진 수열을 만들 수 있는지 검사하는 프로그램을 작성하는 문제이다.
n : 주어진 수열의 길이
dest : 주어진 수열
src : n, n-1, n-2, ..., 2, 1이 저장된 스택
tmp : src로부터 값을 push 받고, 조건을 만족하면 pop하는 스택
ans : push/pop 연산을 저장하는 리스트
import sys
input = sys.stdin.readline
n = int(input())
dest = [int(input()) for _ in range(n)]
src = [i for i in range(n, 0, -1)]
tmp = [] # stack for making dest from src.
ans = [] # storage for push/pop actions.
for now in dest:
while src and src[-1] <= now:
tmp.append(src.pop())
ans.append('+')
if tmp and tmp[-1] == now:
tmp.pop()
ans.append('-')
else:
print('NO')
break
else:
for op in ans:
print(op)