์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๋ฐฑ์ค€ (boj) ํŒŒ์ด์ฌ - 11052 ๋ฒˆ : ์นด๋“œ ๊ตฌ๋งคํ•˜๊ธฐ (DP)

  • -

๋ฌธ์ œ ๋งํฌ : https://www.acmicpc.net/problem/11052

 

11052๋ฒˆ: ์นด๋“œ ๊ตฌ๋งคํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— ๋ฏผ๊ทœ๊ฐ€ ๊ตฌ๋งคํ•˜๋ ค๊ณ  ํ•˜๋Š” ์นด๋“œ์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000) ๋‘˜์งธ ์ค„์—๋Š” Pi๊ฐ€ P1๋ถ€ํ„ฐ PN๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Pi ≤ 10,000)

www.acmicpc.net

 

 

 

 


 

๋ฌธ์ œ ๋ถ„๋ฅ˜ : ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ https://infinitt.tistory.com/246

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ - ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ (Dynamic Programming)

๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ (Dynamic Programming) : DP ๊ฐœ๋… : ๋ฌธ์ œ๋ฅผ ๋” ์ž‘์€ ๋‹จ์œ„๋กœ ์ชผ๊ฐœ์–ด ํ•ด๊ฒฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜. (๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋น„์Šทํ•˜๋‹ค. ์ฐจ์ด์ ์€ ๋ฐ”๋กœ ์•„๋žซ์ค„.) ํ•ต์‹ฌ์€, ๊ทธ ์ž‘์€ ๋‹จ์œ„์˜ ๋ฌธ์ œ๋“ค์ด ๋ฐ˜๋ณตํ•ด์„œ..

infinitt.tistory.com

 

์ž‘์€ ๋ฌธ์ œ๋ถ€ํ„ฐ ์ƒ๊ฐํ•ด๋ณธ๋‹ค. ์นด๋“œ๋ฅผ ํ•œ๊ฐœ ์‚ฌ๋Š”๋ฒ•๋ถ€ํ„ฐ

dp[i] = ์นด๋“œ i๊ฐœ ๊ตฌ๋งคํ•˜๋Š” ์ตœ๋Œ€ ๊ฐ€๊ฒฉ , p[k] = k๊ฐœ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์นด๋“œํŒฉ ๊ฐ€๊ฒฉ ์ด๋ผ๊ณ  ํ–ˆ์„๋•Œ

์นด๋“œ๋ฅผ i๊ฐœ ๊ตฌ๋งคํ•˜๋Š” ์ตœ๋Œ€ ๋น„์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

p[1] + dp[i-1]

p[2] + dp[i-2]

p[3] + dp[i-3]

'''''

'''''

p[i] + dp[0]

 

๋”ฐ๋ผ์„œ ์ ํ™”์‹์€ dp[i] = p[k] + dp[i - k] ๊ฐ€ ๋œ๋‹ค.

 

 

*์ •๋‹ต ์ฝ”๋“œ (Python)
N = int(input())
p = [0] + list(map(int,input().split()))
dp = [0 for _ in range(N+1)]


for i in range(1,N+1):
    for k in range(1,i+1):
        dp[i] = max(dp[i], dp[i-k] + p[k])
print(dp[i])

 

 

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.