๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ์ •์ˆ˜ ์‚ผ๊ฐํ˜• BOJ #1932

by seolhee2750 2021. 12. 17.
๋ฌธ์ œ

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

 

1932๋ฒˆ: ์ •์ˆ˜ ์‚ผ๊ฐํ˜•

์ฒซ์งธ ์ค„์— ์‚ผ๊ฐํ˜•์˜ ํฌ๊ธฐ n(1 ≤ n ≤ 500)์ด ์ฃผ์–ด์ง€๊ณ , ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ n+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ ์ •์ˆ˜ ์‚ผ๊ฐํ˜•์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
n = int(input())
memory = [int(input())]

for _ in range(1, n):
    now = list(map(lambda x: int(x), input().split()))
    for i in range(len(now)):
        if i == 0:
            now[0] += memory[0]
        elif i == len(now)-1:
            now[-1] += memory[-1]
        else:
            if memory[i-1] > memory[i]: now[i] += memory[i-1]
            else: now[i] += memory[i]
    memory = now

print(max(memory))

๐Ÿ‘‰ DP ๋ฌธ์ œ๋กœ, ์ตœ์†Œํ•œ์˜ ํƒ์ƒ‰์œผ๋กœ ์ตœ๋Œ€ ๊ฐ’์„ ๋งŒ๋“œ๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ํฌ์ธํŠธ!

  • ๋ชจ๋“  ์ธต์—์„œ๋Š” ์ตœ๋Œ“๊ฐ’์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•˜์—ฌ ์œ— ์ธต์˜ ๋Œ€๊ฐ์„  ์•ž, ํ˜น์€ ๋’ค ์ค‘ ๋” ํฐ ๋ˆ„์ ํ•ฉ์˜ ์ˆ˜๋ฅผ ๊ณจ๋ผ์•ผ ํ•œ๋‹ค.
    #1149_RGB๊ฑฐ๋ฆฌ ๋ฌธ์ œ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ค ์ž…๋ ฅ์ด ์žˆ๋“  3๊ฐ€์ง€์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ ์ถ”๋ฆด ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—
    ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ชจ๋‘ ์ƒ์„ฑํ•ด๋†“๊ณ  ์‹œ์ž‘ํ–ˆ์ง€๋งŒ, ์ด ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ์—๋Š”
    ์ž…๋ ฅ๋˜๋Š” N๊ฐ’์— ๋”ฐ๋ผ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋ฌด์ˆ˜ํžˆ ๋งŽ์•„์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ 
    '์œ— ์ธต(์ด์ „ ์ธต)'์„ ์˜๋ฏธํ•˜๋Š” memory ๋ฆฌ์ŠคํŠธ์™€ '์•„๋žซ ์ธต(ํ˜„์žฌ ์ธต)'์„ ์˜๋ฏธํ•˜๋Š” now ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ,
    ๊ณ„์†ํ•˜์—ฌ memory ๋ฆฌ์ŠคํŠธ์— ๊ฐ’์„ ๋ˆ„์ ํ•˜๋ฉฐ ๋งˆ์ง€๋ง‰์— ์ €์žฅ๋œ memory ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๊ณ ๋ฅด๋ฉด ๋œ๋‹ค.
  • ํ•œ ๊ฐ€์ง€ ๊ณ ๋ คํ•ด์ค„ ์ ์€, ํ•œ ์ธต์˜ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋‚˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ์˜ ๊ฒฝ์šฐ์—๋Š”
    ์œ— ์ธต์—์„œ ๋‘ ๊ฐ€์ง€ ๊ฐ’์„ ์„ ํƒํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๋ถ€๋ถ„๋งŒ ๋”ฐ๋กœ ์˜ˆ์™ธ๋กœ ๋นผ์ฃผ์–ด์„œ ์ฒ˜๋ฆฌํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ค„ ๊ฒƒ์ธ์ง€๋งŒ ์ƒ๊ฐํ•˜๋ฉด ์‰ฌ์šด ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™๋‹ค.

๋Œ“๊ธ€