SHyeonL / Future_Internet_LAB_Algorithm

미래 인터넷 연구실 알고리즘 스터디
0 stars 0 forks source link

1874 - 스택 수열 #7

Open SHyeonL opened 1 year ago

SHyeonL commented 1 year ago
import sys

input = sys.stdin.readline

m = 0
n = int(input())
ans = ''
stack = []
for i in range(n):
    x = int(input())
    if x > m:
        while x > m:
            m += 1
            stack.append(m)
            ans += '+'
        stack.pop()
        ans += '-'
    else:
        found = False
        if stack:
            top = stack.pop()
            ans += '-'
            if x == top:
                found = True
        if not found:
            print("NO")
            exit()
for i in ans:
    print(i)
yuneojin commented 1 year ago
n = int(input())
answer = []
stack = []
flag = 0
m = 1

for i in range(n):
    num = int(input())
    while m<=num:
        stack.append(m)
        answer.append("+")
        m += 1

    if stack[-1] == num:
        stack.pop()
        answer.append("-")
    else:
        print("NO")
        flag = 1
        break

if flag ==0:
    for i in answer:
        print(i)