ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

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

 

๋ฌธ์ œ ํ’€์ด

 ์ด ๋ฌธ์ œ๋Š” ๋ฌธ์ œ๊ฐ€ ์–ด๋ ค์šด๊ฒŒ ์•„๋‹ˆ๋ผ ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ์ ‘ํ–ˆ์„ ๋•Œ... ๋กธ....? ์ด๊ฒŒ ๋จผ์†Œ๋ฆฌ... ๐Ÿ˜ฃ ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ์ฐฌ์ฐฌํžˆ ์ฝ์œผ๋‹ˆ ์ด๊ฑด๊ฐ€? ํ•˜๊ณ  ์ดํ•ดํ–ˆ๋‹ค. ์˜ˆ์ œ ์ž…๋ ฅ 4, 3, 6, 8, 7 ,5, 2, 1์ด ์ฃผ์–ด์ง€๋ฉด stack์— 1 - n๊นŒ์ง€์˜ ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ push, popํ•˜์—ฌ์„œ ์ €๋Ÿฌํ•œ ์ˆœ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๊ฐ€๋ฅผ ๋ฌป๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

 ์ˆœ์—ด์˜ ์ฒ˜์Œ์ด 4, 3์ด ๋˜๋ ค๋ฉด stack์— 1, 2, 3, 4๋ฅผ ์ˆœ์„œ๋Œ€๋กœ pushํ•˜๊ณ  pop์ด 2๋ฒˆ ๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋‹ค์Œ 6, 8์ด ์ถœ๋ ฅ ๋˜๋ ค๋ฉด 5, 6์„ pushํ•˜๊ณ  pop์ด 1๋ฒˆ ๋˜์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ๋‹ค์‹œ ์ •๋ฆฌํ•ด๋ณด์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  • ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ํ•ด๋‹น ๊ฐ’ ๊นŒ์ง€์˜ 1 ๋ถ€ํ„ฐ ์ž…๋ ฅ๊ฐ’๊นŒ์ง€ stack์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
    • ๊ฐ’์ด ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์นด์šดํŠธ๋กœ ์ž…๋ ฅ๋œ ๊ฐ’์„ ํ™•์ธํ•œ๋‹ค.
  • stack์˜ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ถ”๊ฐ€๋œ ๊ฐ’์ด ์ž…๋ ฅ๊ฐ’๊ณผ ๊ฐ™์œผ๋ฉด pop์„ ํ•œ๋‹ค.
    • ์•„๋‹ˆ๋ผ๋ฉด ์ˆœ์—ด์„ ์ด์šฉํ•ด push popํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ตฌ์„ฑ์ด๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


def solution(n):
    count = 1
    stack = []
    answer = []

    for _ in range(n):
        num = int(stdin.readline())

        while count <= num:
            stack.append(count)
            answer.append('+')
            count += 1

        if stack[-1] == num:
            stack.pop()
            answer.append('-')
        else:
            return None
    return answer

if __name__ == "__main__":
    n = int(stdin.readline())
    ret = solution(n)
    if ret is None:
        print("NO")
    else:
        print('\n'.join(num for num in ret))
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€