ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/9465
package main
import (
"bufio"
"fmt"
"math"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var t int
fmt.Fscanln(reader, &t)
for i := 0; i < t; i++ {
var n int
fmt.Fscanln(reader, &n)
var scores = make([][]int, 2)
var dp = make([][]int, 2)
for j := 0; j < 2; j++ {
scores[j] = make([]int, n)
dp[j] = make([]int, n)
for k := 0; k < n; k++ {
fmt.Fscanf(reader, "%d ", &scores[j][k])
}
}
dp[0][0] = scores[0][0]
dp[1][0] = scores[1][0]
for j := 1; j < n; j++ {
if j == 1 {
dp[0][j] = dp[1][j-1] + scores[0][j]
dp[1][j] = dp[0][j-1] + scores[1][j]
} else {
dp[0][j] = int(math.Max(float64(dp[1][j-1]), float64(dp[1][j-2]))) + scores[0][j]
dp[1][j] = int(math.Max(float64(dp[0][j-1]), float64(dp[0][j-2]))) + scores[1][j]
}
}
fmt.Fprintln(writer, int(math.Max(float64(dp[0][n-1]), float64(dp[1][n-1]))))
}
}
๐จ Python3
# https://www.acmicpc.net/problem/9465
import sys
if __name__ == "__main__":
t = int(sys.stdin.readline())
for i in range(t):
n = int(sys.stdin.readline())
scores = []
dp = [[0]*n for _ in range(2)]
for j in range(2):
scores.append(list(map(int, sys.stdin.readline().split())))
dp[0][0] = scores[0][0]
dp[1][0] = scores[1][0]
for j in range(1, n):
if j == 1:
dp[0][j] = dp[1][j-1] + scores[0][j]
dp[1][j] = dp[0][j-1] + scores[1][j]
else:
dp[0][j] = max(dp[1][j-1], dp[1][j-2]) + scores[0][j]
dp[1][j] = max(dp[0][j-1], dp[0][j-2]) + scores[1][j]
print(max(dp[0][n-1], dp[1][n-1]))
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 6603๋ฒ / ๋ก๋ [Go][Python3] (0) | 2021.03.22 |
---|---|
BOJ / 1182๋ฒ / ๋ถ๋ถ์์ด์ ํฉ [Go][Python3] (0) | 2021.03.21 |
BOJ / 2294๋ฒ / ๋์ 2 [Go][Python3] (0) | 2021.03.19 |
BOJ / 11052๋ฒ / ์นด๋ ๊ตฌ๋งคํ๊ธฐ [Go][Python3] (0) | 2021.03.18 |
BOJ / 1697๋ฒ / ์จ๋ฐ๊ผญ์ง [Go][Python3] (0) | 2021.03.17 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ์๊ฐ๊ต์ฒด
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ๋ฐ๋
- ์ด๋ถํ์
- ํด์๋งต
- go
- ๋ธ๋ฃจํธํฌ์ค
- dfs
- dp
- Golang
- ๋งฅ๋ถ
- baekjoon
- ํ๋ก์ด๋์์ฌ
- Algorithm
- ์คํ
- Macbook pro 2012 mid 13
- ๋ถํ ์ ๋ณต
- BFS
- ๋งฅ๋ถํ๋ก
- ballet
- MongoDB
- ๋ฐฑ์ค
- AWS
- python3
- ๋ชฝ๊ณ ๋๋น
- ์๋ฐ
- BOJ
- java
- ํ
- ์๊ณ ๋ฆฌ์ฆ
- Total
- Today
- Yesterday