์ƒˆ์†Œ์‹

๐Ÿงฎ PS

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

  • -

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

 

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

์ฒซ์งธ ์ค„์— ๋ฏผ๊ทœ๊ฐ€ ๊ตฌ๋งคํ•˜๋ ค๊ณ  ํ•˜๋Š” ์นด๋“œ์˜ ๊ฐœ์ˆ˜ 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

 

 

 

 

 

 

์นด๋“œ ๊ตฌ๋งคํ•˜๊ธฐ 1 ์„ ๋จผ์ € ํ’€์—ˆ๋‹ค๋ฉด ์–ด๋ ต์ง€ ์•Š์€ ๋ฌธ์ œ๊ฐ™๋‹ค.

์นด๋“œ ๊ตฌ๋งคํ•˜๊ธฐ 1์€ ์ตœ๋Œ€๊ฐ’(max)๋ฅผ ๊ตฌํ•˜๊ณ 

2๋Š” ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•œ๋‹ค๋Š” ์ฐจ์ด๋ฐ–์— ์—†๋‹ค. ์ ํ™”์‹๋„ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” ์ƒ๋žต..

https://infinitt.tistory.com/250

 

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

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

infinitt.tistory.com

 

*์ •๋‹ต์ฝ”๋“œ (Python)
N = int(input())

p = [0] + list(map(int,input().split()))
dp = [False for _ in range(N+1)]


for i in range(1, N+1):
    for k in range(1, i+1):
        if dp[i] == False :
            dp[i] = dp[i-k]+p[k]
        else :
            dp[i] = min(dp[i], dp[i-k]+p[k])

print(dp[N])

(์Šต๊ด€์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”์‹œ์ผœ ์„ ์–ธํ–ˆ์ง€๋งŒ, minํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, False๋กœ ๊ฐ’์„ ๋ฐ”๊ฟ”์คฌ๋‹ค.)

 

 

Contents

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

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