Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 14501๋ฒˆ - ํ‡ด์‚ฌ (๋‘๋ฒˆ์งธ ํ’€์ด, DP)

๊ฐ์ž ๐Ÿฅ” 2023. 1. 20. 11:24
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ

https://www.acmicpc.net/problem/14501

 

14501๋ฒˆ: ํ‡ด์‚ฌ

์ฒซ์งธ ์ค„์— ๋ฐฑ์ค€์ด๊ฐ€ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ด์ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

๐ŸŸ  ๋ฌธ์ œ ํ’€์ด

์ด์ „์— ๋ดค๋˜ ๋ฌธ์ œ์˜€๋˜๊ฒŒ ๊ธฐ์–ต๋‚ฌ๋‹ค. ๋ฌธ์ œ๋Š” ๋”ฑ ๋ณด์ž๋งˆ์ž dp ์ž„์„ ํŒŒ์•… ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๋‚ด๊ฐ€ ํ‰์†Œ์— ์ข‹์•„ํ•˜๋˜ dp ์œ ํ˜•์ด์—ˆ๋‹ค!

์ด๋ ‡๊ฒŒ ํ’€์ด๋ฅผ ์ƒ๊ฐํ•ด๋‚ด๊ณ , ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

let n = Int(readLine()!)!
var arr = [[0, 0]]
for _ in 0..<n {
    arr.append(readLine()!.split(separator: " ").map{ Int($0)! })
}
var dp = Array(repeating: 0, count: 21) // ์ตœ๋Œ€ N -> 15์ผ, ์ƒ๋‹ด term ์ตœ๋Œ€ 5์ผ

for i in 1..<n+1 {
    if i+arr[i][0] > n {
        continue
    }
    dp[i+arr[i][0]] = max(arr[i][1] + dp[i], dp[i])
}

print(dp.max()!)

์ด๋ ‡๊ฒŒ ํ‘ธ๋‹ˆ๊นŒ ํ‹€๋ ธ๋‹ค๊ณ  ๋œจ๋Š” ๊ฒƒ์ด๋‹ค.... ์™œ์ง€??

๊ณฐ๊ณฐํžˆ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ, ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋Š” ํŒ๋‹จํ•˜์ง€ ์•Š์•˜๋‹ค. 4์ผ์ฐจ์— ์ƒˆ๋กœ์šด ์ƒ๋‹ด์„ ์ง„ํ–‰ํ•˜์ง€ ์•Š๊ณ , ์ด์ „์˜ ๊ฐ’์ด ์œ ์ง€๋˜๋Š”๊ฒŒ ๋” ํฐ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ์ด๋‹ค.

์ด๋Ÿฐ ๊ฒฝ์šฐ๊นŒ์ง€ ๋ชจ๋‘ ํฌํ•จ์‹œ์ผœ์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผํ–ˆ๋‹ค. ๋‹ค์Œใ„ด

let n = Int(readLine()!)!
var arr = [[0, 0]]
for _ in 0..<n {
    arr.append(readLine()!.split(separator: " ").map{ Int($0)! })
}
var dp = Array(repeating: 0, count: 21) // ์ตœ๋Œ€ N -> 15์ผ, ์ƒ๋‹ด term ์ตœ๋Œ€ 5์ผ

for i in 1..<n+1 {
    if i+arr[i][0] > n+1 {
        continue
    }

    if dp[i] > dp[i+1] {
        dp[i+1] = dp[i]
    }

    dp[i+arr[i][0]] = max(arr[i][1] + dp[i], dp[i+arr[i][0]])
}
print(dp.max()!)

์ด๋ ‡๊ฒŒ ๊ฐ€์šด๋ฐ if ๋ฌธ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค. ๋‚ด์ผ์˜ ๊ธˆ์•ก์ด ์˜ค๋Š˜์˜ ๊ธˆ์•ก๋ณด๋‹ค ์ž‘์œผ๋ฉด, ์˜ค๋Š˜์˜ ๊ธˆ์•ก์„ ๊ทธ๋Œ€๋กœ ์ด์›”(?)ํ•ด์„œ ๊ฐ€์ ธ๊ฐ€๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— dp[i+1]์— dp[i]๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ๋˜ ํ‹€๋ ธ๋‹ค.... ์™œ... ๋„๋Œ€์ฒด ๋˜ ์™œ .. ๋ญ๊ฐ€๋ฌธ์  ๋ฐ... ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋‹ˆ๊นŒ, ์ฒซ๋ฒˆ์งธ if๋ฌธ์ด ๋ฌธ์ œ์˜€๋‹ค. for๋ฌธ์— ์ง„์ž…ํ•˜์ž๋งˆ์ž if๋ฅผ ํ†ตํ•ด์„œ n+1์ด ๋„˜์–ด๊ฐ€๋Š”์ง€ ํŒ๋‹จ์„ ํ•ด์ฃผ๊ณ , ๋„์„๊ฒฝ์šฐ continueํ•ด์„œ ํ•ด๋‹น i๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋„๋ก ํ•ด์คฌ๋‹ค. ํ•˜์ง€๋งŒ, ์ด๊ฒƒ์€ ๋งŒ์•ฝ n+1์„ ๋„˜๋”๋ผ๋„, dp[i]๋Š” ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ์–ด์•ผํ•˜๋Š”๋ฐ, ์ด ๋ชจ๋“  ๊ณผ์ •์„ ๋‚ ๋ ค๋จน๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ’์ด ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ฌ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค. ๊ทธ๋ž˜์„œ if ๋ฌธ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”์ค˜์•ผ ํ–ˆ๋‹ค. (๋ฐ”๊พธ๋‹ˆ๊นŒ ์ •๋‹ต์ด๋‹ค!) 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

์•„,, ์ค‘๊ฐ„์ค‘๊ฐ„ ์ธ๋ฑ์Šค ๊ด€๋ จํ•ด์„œ ์˜ค๋ฅ˜๋„ ๋งŽ์•˜๊ณ , ์ด๊ฒƒ์ €๊ฒƒ if๋ฌธ์— ๋Œ€ํ•ด์„œ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•œ ๋ถ€๋ถ„...์ด ๋งŽ์•˜์–ด์„œ ๊ต‰์žฅํžˆ ๋งŽ์ด ํ‹€๋ ธ๋‹ค. ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ํ•˜... ๋‘๋ฒˆ์งธ ํ’€์ด์ธ๋ฐ ์™ค์ผ€ ์ฉ”์ฉ”๋งค๋Š”๊ฑฐ์ง€ ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ํ•ด๋ƒˆ๋‹ค!

import Foundation

let n = Int(readLine()!)!
var arr = [[0, 0]]
for _ in 0..<n {
    arr.append(readLine()!.split(separator: " ").map{ Int($0)! })
}
var dp = Array(repeating: 0, count: 100)

for i in 1..<n+1 {
    if dp[i] > dp[i+1] {
        dp[i+1] = dp[i]
    }

    if i+arr[i][0] > n+1 {
        continue
    }

    dp[i+arr[i][0]] = max(arr[i][1] + dp[i], dp[i+arr[i][0]])
}

print(dp.max()!)

 

dp.max()!๊ฐ’์„ ๋ฝ‘์ง€ ์•Š๊ณ , dp[n+1]๊ฐ’์„ ๋ฝ‘์•„๋„ ๋œ๋‹ค~ 

๋ฐ˜์‘ํ˜•