ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
12789๋ฒ: ๋ํค๋ํค ๊ฐ์๋๋ฆฌ๋ฏธ
์ธํ๋ํ๊ต ํ์ํ์์๋ ์ค๊ฐ, ๊ธฐ๋ง๊ณ ์ฌ ๋๋ง๋ค ์ํ ๊ณต๋ถ์ ์ง์น ํ์ฐ๋ค์ ์ํด ๊ฐ์์ ๋๋ ์ฃผ๋ ๊ฐ์ ๋๋ฆฌ๋ฏธ ํ์ฌ๋ฅผ ์ค์ํ๋ค. ์นํ์ด๋ ์ํ ๊ธฐ๊ฐ์ด ๋ ๋๋ง๋ค ๊ฐ์์ ๋ฐ์ ์๊ฐ์ ๋๊ทผ๋
www.acmicpc.net
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/12789
package main
import (
"bufio"
"fmt"
"os"
"sort"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var students = make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscanf(reader, "%d ", &students[i])
}
fmt.Fprintln(writer, getResult(students))
}
func getResult(students []int) string {
var stack []int
var line []int
for len(students) > 0 {
if len(stack) == 0 {
stack = append(stack, students[0])
students = students[1:]
} else {
if stack[len(stack)-1] > students[0] {
stack = append(stack, students[0])
students = students[1:]
} else {
line = append(line, stack[len(stack)-1])
students = students[1:]
}
}
}
for len(stack) > 0 {
line = append(line, stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
if sort.SliceIsSorted(line, func(i, j int) bool {
return line[i] < line[j]
}) {
return "Nice"
}
return "Sad"
}
๐จ Python3
# https://www.acmicpc.net/problem/12789
import sys
def get_result(students):
stack = []
line = []
while len(students) > 0:
if len(stack) == 0:
stack.append(students.pop(0))
else:
if stack[-1] > students[0]:
stack.append(students.pop(0))
else:
line.append(stack[-1])
students.pop(0)
while len(stack) > 0:
line.append(stack[-1])
stack.pop(-1)
if line == sorted(line):
return "Nice"
return "Sad"
if __name__ == "__main__":
n = int(sys.stdin.readline())
students = list(map(int, sys.stdin.readline().rstrip().split()))
print(get_result(students))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 10994๋ฒ / ๋ณ ์ฐ๊ธฐ 19 [Go][Python3] (0) | 2021.02.25 |
---|---|
BOJ / 20301๋ฒ / ๋ฐ์ ์์ธํธ์ค [Go][Python3] (0) | 2021.02.24 |
BOJ / 6187๋ฒ / Going to the Movies [Go][Python3] (0) | 2021.02.22 |
BOJ / 3048๋ฒ / ๊ฐ๋ฏธ [Go][Python3] (0) | 2021.02.21 |
BOJ / 3085๋ฒ / ์ฌํ ๊ฒ์ [Go][Python3] (0) | 2021.02.20 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ์๊ฐ๊ต์ฒด
- ballet
- ํ๋ก์ด๋์์ฌ
- dfs
- ์๋ฐ
- BOJ
- Algorithm
- BFS
- ํด์๋งต
- ๋ฐ๋
- ์ด๋ถํ์
- ๋งฅ๋ถ
- ์คํ
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋ฐฑ์ค
- ์๊ณ ๋ฆฌ์ฆ
- python3
- ๋ชฝ๊ณ ๋๋น
- dp
- java
- Golang
- MongoDB
- ๋ถํ ์ ๋ณต
- ํ
- go
- ๋ธ๋ฃจํธํฌ์ค
- ๋งฅ๋ถํ๋ก
- Macbook pro 2012 mid 13
- AWS
- baekjoon
- Total
- Today
- Yesterday