ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ 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
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 2164๋ฒ / ์นด๋2 [Go][Python3] (0) | 2020.11.10 |
---|---|
BOJ / 18258๋ฒ / ํ 2 [Go][Python3] (0) | 2020.11.09 |
BOJ / 4949๋ฒ / ๊ท ํ์กํ ์ธ์ [Go][Python3] (0) | 2020.11.07 |
BOJ / 9012๋ฒ / ๊ดํธ [Go][Python3] (0) | 2020.11.06 |
BOJ / 2004๋ฒ / ์กฐํฉ 0์ ๊ฐ์ [Go][Python3] (0) | 2020.11.05 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- AWS
- ์๋ฐ
- ์ด๋ถํ์
- ๋งฅ๋ถํ๋ก
- ballet
- dp
- ๋ถํ ์ ๋ณต
- ๋ชฝ๊ณ ๋๋น
- ์๊ฐ๊ต์ฒด
- ์๊ณ ๋ฆฌ์ฆ
- BOJ
- dfs
- Algorithm
- baekjoon
- ๋ธ๋ฃจํธํฌ์ค
- ํด์๋งต
- ํ๋ก์ด๋์์ฌ
- ๋งฅ๋ถ
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- go
- Macbook pro 2012 mid 13
- ์คํ
- ๋ฐ๋
- Golang
- python3
- ํ
- ๋ฐฑ์ค
- MongoDB
- java
- BFS
- Total
- Today
- Yesterday