[BOJ][๐ŸŸก4][๋ฐฑ์ค€#02228] ๊ตฌ๊ฐ„ ๋‚˜๋ˆ„๊ธฐ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2228


๋ฌธ์ œ

N(1 โ‰ค N โ‰ค 100)๊ฐœ์˜ ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ 1์ฐจ์› ๋ฐฐ์—ด์ด ์žˆ๋‹ค. ์ด ๋ฐฐ์—ด์—์„œ M(1 โ‰ค M โ‰ค โŒˆ(N/2)โŒ‰)๊ฐœ์˜ ๊ตฌ๊ฐ„์„ ์„ ํƒํ•ด์„œ, ๊ตฌ๊ฐ„์— ์†ํ•œ ์ˆ˜๋“ค์˜ ์ด ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋„๋ก ํ•˜๋ ค ํ•œ๋‹ค. ๋‹จ, ๋‹ค์Œ์˜ ์กฐ๊ฑด๋“ค์ด ๋งŒ์กฑ๋˜์–ด์•ผ ํ•œ๋‹ค.

๊ฐ ๊ตฌ๊ฐ„์€ ํ•œ ๊ฐœ ์ด์ƒ์˜ ์—ฐ์†๋œ ์ˆ˜๋“ค๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๊ตฌ๊ฐ„๋ผ๋ฆฌ ๊ฒน์ณ์žˆ๊ฑฐ๋‚˜ ์ธ์ ‘ํ•ด ์žˆ์–ด์„œ๋Š” ์•ˆ ๋œ๋‹ค. ์ •ํ™•ํžˆ M๊ฐœ์˜ ๊ตฌ๊ฐ„์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. M๊ฐœ ๋ฏธ๋งŒ์ด์–ด์„œ๋Š” ์•ˆ ๋œ๋‹ค.

N๊ฐœ์˜ ์ˆ˜๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‹ต์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋‘ ์ •์ˆ˜ N, M์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๋ฐฐ์—ด์„ ์ด๋ฃจ๋Š” ์ˆ˜๋“ค์ด ์ฐจ๋ก€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋ฐฐ์—ด์„ ์ด๋ฃจ๋Š” ์ˆ˜๋“ค์€ -32768 ์ด์ƒ 32767 ์ดํ•˜์˜ ์ •์ˆ˜์ด๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

6 2
-1
3
1
2
4
-1


์ถœ๋ ฅ

9


My Sol

import sys
# sys.stdin = open('t.txt')
input = sys.stdin.readline

def foo(part, pe):
    global N, M, ans, ssum
    s = e = pe + 2

    if part == M:
        while s < N:
            if e <= N:
                ssum[part] = sarr[e] - sarr[s-1]
                ans = max(ans, sum(ssum))
                e += 1
            else:
                s += 1
                e = s
        return

    while s < N:
        if e <= N:
            ssum[part] = sarr[e] - sarr[s-1]
            foo(part+1, e)
            e += 1
        else:
            s += 1
            e = s


N, M = map(int, input().split())
arr = [int(input()) for _ in range(N)]
sarr = [0]*(N+1)
for i in range(1, N+1):
    sarr[i] = sarr[i-1] + arr[i-1]

ssum = [0]*(M+1)
ans = -1e7
foo(1, -1)
print(ans)

์žฌ๊ท€์™€ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•ด ์ง€์ •๋œ ๊ฐœ์ˆ˜์˜ ๊ตฌ๊ฐ„๋งˆ๋‹ค์˜ ํ•ฉ์„ ๊ตฌํ•ด ans์— ์ตœ๋Œ“๊ฐ’์„ ๋งค๋ฒˆ ๊ฐฑ์‹ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.


๊ฒฐ๊ณผ

์‹œ๊ฐ„์ดˆ๊ณผ

์•„๋ฌด๋ž˜๋„ ๊ตฌ๊ฐ„์ด ๋งŽ์•„์ง€๋ฉด sarr์—๋„ ๊ฐ’์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๊ณ , ๊ทธ๋งŒํผ sumํ•˜๊ณ  ์žฌ๊ท€ ๋‹จ๊ณ„๋ฅผ ์˜ค๊ฐ€๋Š” ๊ณผ์ •์ด ๋งŽ์•„์ง€๋‹ค๋ณด๋‹ˆ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค.


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

import sys
input = sys.stdin.readline

n, m = map(int, input().split())
con = [[0]+[-1e9]*m for i in range(n+1)]
notcon = [[0]+[-1e9]*m for i in range(n+1)]

for i in range(1, n+1):
    num = int(input())
    for j in range(1, min(m, (i+1)//2)+1):
        notcon[i][j]=max(con[i-1][j], notcon[i-1][j])
        con[i][j]=max(con[i-1][j], notcon[i-1][j-1])+num
print(max(con[n][m], notcon[n][m]))

ํŠน์ • idx๊นŒ์ง€ n๊ฐœ์˜ ๊ตฌ๊ฐ„ํ•ฉ์„ ๊ตฌํ•˜๊ณ ์ž ํ•  ๋•Œ, max(์•ž์˜ ์›์†Œ๋ฅผ ์ œ์™ธํ•œ n-1๊ฐœ์˜ ์ตœ๋Œ€ ๊ตฌ๊ฐ„ํ•ฉ, ์•ž์˜ ์›์†Œ๋ฅผ ํฌํ•จํ•œ n๊ฐœ์˜ ์ตœ๋Œ€ ๊ตฌ๊ฐ„ํ•ฉ)+ํ˜„์žฌ๊ฐ’์„ ์ ์šฉํ–ˆ๋‹ค๊ณ  ํ•œ๋‹ค. ์–ด๋–ป๊ฒŒ ์ด๋Ÿฐ ๋ฐœ์ƒ์„โ€ฆ. dp๋Š” ์ •๋ง ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™๋‹ค.

j์˜ ๋ฒ”์œ„๋Š” ์™œ range(1, min(m, (i+1)//2)+1)์ธ๊ฑธ๊นŒ. ๊ตฌ๊ฐ„์ด 1๊ฐœ๋ถ€ํ„ฐ ๊ฐ€๋Šฅํ•œ ๊ตฌ๊ฐ„์˜ ๊ฐœ์ˆ˜๋ฅผ ๋”ฐ์ ธ์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๊ตฌ๊ฐ„์˜ ๊ฐœ์ˆ˜ j๋Š” m์„ ๋„˜๊ธธ ์ˆ˜ ์—†๊ณ , ๋งŽ์•„๋„ (i+1//2)์„ ๋„˜๊ธธ ์ˆ˜ ์—†๋‹ค. ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ์ œ์•ฝ์„ ๋‘์ง€ ์•Š์•˜์„๊นŒ.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ