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

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

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


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1874
// ์Šคํƒ์„ ํ™œ์šฉํ•˜๋Š” ๋ฌธ์ œ
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)

	var stackNum int = 1
	var stackSequence []int
	var result []string
	for i := 0; i < n; i++ {
		var input int
		fmt.Fscanln(reader, &input)
		if len(stackSequence) > 0 && input > stackSequence[len(stackSequence)-1] || input > stackNum {
			for j := stackNum; j < input+1; j++ {
				stackSequence = append(stackSequence, j)
				result = append(result, "+")
				stackNum++
			}
			stackSequence = stackSequence[:len(stackSequence)-1]
			result = append(result, "-")
		} else if input == stackNum {
			stackSequence = append(stackSequence, input)
			result = append(result, "+")
			stackNum++
			stackSequence = stackSequence[:len(stackSequence)-1]
			result = append(result, "-")
		} else if len(stackSequence) > 0 && input == stackSequence[len(stackSequence)-1] {
			stackSequence = stackSequence[:len(stackSequence)-1]
			result = append(result, "-")
		}
	}

	if len(stackSequence) > 0 {
		fmt.Fprintln(writer, "NO")
	} else {
		for _, v := range result {
			fmt.Fprintln(writer, v)
		}
	}
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1874
# ์Šคํƒ์„ ํ™œ์šฉํ•˜๋Š” ๋ฌธ์ œ
import sys

if __name__ == "__main__":
    n = int(sys.stdin.readline())

    stack_num = 1
    stack_seq = []
    result = []
    for i in range(n):
        val = int(sys.stdin.readline())
        if len(stack_seq) > 0 and val > stack_seq[-1] or val > stack_num:
            for j in range(stack_num, val+1):
                stack_seq.append(j)
                result.append("+")
                stack_num += 1
            stack_seq = stack_seq[:-1]
            result.append("-")
        elif val == stack_num:
            stack_seq.append(val)
            result.append("+")
            stack_num += 1
            stack_seq = stack_seq[:-1]
            result.append("-")
        elif len(stack_seq) > 0 and val == stack_seq[-1]:
            stack_seq = stack_seq[:-1]
            result.append("-")

    if len(stack_seq) > 0:
        print("NO")
    else:
        for v in result:
            print(v)
728x90
๋Œ“๊ธ€