๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][BAEKJOON][DP] 2156๋ฒˆ ํฌ๋„์ฃผ ์‹œ์‹

ibelieveinme 2021. 8. 5. 23:17
728x90

[๋ฌธ์ œ]

 

2156๋ฒˆ: ํฌ๋„์ฃผ ์‹œ์‹

ํšจ์ฃผ๋Š” ํฌ๋„์ฃผ ์‹œ์‹ํšŒ์— ๊ฐ”๋‹ค. ๊ทธ ๊ณณ์— ๊ฐ”๋”๋‹ˆ, ํ…Œ์ด๋ธ” ์œ„์— ๋‹ค์–‘ํ•œ ํฌ๋„์ฃผ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํฌ๋„์ฃผ ์ž”์ด ์ผ๋ ฌ๋กœ ๋†“์—ฌ ์žˆ์—ˆ๋‹ค. ํšจ์ฃผ๋Š” ํฌ๋„์ฃผ ์‹œ์‹์„ ํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€ ๊ทœ

www.acmicpc.net

 

[์„ค๋ช…]

โ˜…์ด ๋ฌธ์ œ๊ฐ€ DP ์ธ ์ด์œ : ํ˜„์žฌ ๊ฐ’์„ ๊ตฌํ•  ๋•Œ ์ด์ „ ๊ฒฐ๊ณผ๊ฐ’์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ์–‘์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ

  •  ํฌ๋„์ฃผ๊ฐ€ 1์ž” ์ผ ๋•Œ,
    1์ž” ๋ชจ๋‘๋ฅผ ์„ ํƒํ•ด์•ผ ์ตœ๋Œ€์ด๋‹ค.

 

  • ํฌ๋„์ฃผ๊ฐ€ 2์ž” ์ผ ๋•Œ,
    2์ž” ๋ชจ๋‘๋ฅผ ์„ ํƒํ•ด์•ผ ์ตœ๋Œ€์ด๋‹ค.

 

  • ํฌ๋„์ฃผ๊ฐ€ 3์ž” ์ผ ๋•Œ,
    3๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•  ๊ฒฝ์šฐ์™€ ์„ ํƒํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.
    ์ด ๋•Œ, 3๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ๋Š” ํฌ๋„์ฃผ๊ฐ€ 2์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’๊ณผ ๊ฐ™๋‹ค.
    3๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ–ˆ๋‹ค๋ฉด, 2๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๊ณ  1๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์™€ โ–ก โ–  โ– , 2๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜์ง€ ์•Š๊ณ  1๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. โ–  โ–ก โ– 

 

  • ํฌ๋„์ฃผ๊ฐ€ 4์ž” ์ผ ๋•Œ,
    4๋ฒˆ์จฐ ์ž”์„ ์„ ํƒํ•  ๊ฒฝ์šฐ์™€ ์„ ํƒํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.
    4๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” ํฌ๋„์ฃผ๊ฐ€ 3์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’๊ณผ ๊ฐ™๋‹ค.
    4๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ๋Š” 3๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๊ณ  1๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ์™€(=ํฌ๋„์ฃผ๊ฐ€ 1์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’) โ–  โ–ก โ–  โ– , 3๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜์ง€ ์•Š๊ณ  ํฌ๋„์ฃผ๊ฐ€ 2์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’์„ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. โ–  โ–  โ–ก โ– 

 

  • ํฌ๋„์ฃผ๊ฐ€ 5์ž” ์ผ ๋•Œ,
    5๋ฒˆ์จฐ ์ž”์„ ์„ ํƒํ•  ๊ฒฝ์šฐ์™€ ์„ ํƒํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.
    5๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” ํฌ๋„์ฃผ๊ฐ€ 4์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’๊ณผ ๊ฐ™๋‹ค.
    5๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ๋Š” 4๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๊ณ  2๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ์™€(=ํฌ๋„์ฃผ๊ฐ€ 2์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’) โ–ก โ–  โ–ก โ–  โ– , 4๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜์ง€ ์•Š๊ณ  ํฌ๋„์ฃผ๊ฐ€ 3์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’์„ ์ด์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. (โ–ก โ–  โ–  โ–ก โ– ), (โ–  โ–  โ–ก โ–ก โ– ), (โ–  โ–ก โ–  โ–ก โ– )

 

์ฆ‰, ํ˜„์žฌ ๊ฐ’์„ ๊ตฌํ•  ๋•Œ ์ด์ „ ๊ฒฐ๊ณผ๊ฐ’์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ์–‘์ด ๋‚˜์˜จ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์œ„์˜ ๊ณผ์ •์„ ์ ํ™”์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

 

  • ํฌ๋„์ฃผ๊ฐ€ N์ž” ์ผ ๋•Œ,
    N๋ฒˆ์จฐ ์ž”์„ ์„ ํƒํ•  ๊ฒฝ์šฐ์™€ ์„ ํƒํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.
    N๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” ํฌ๋„์ฃผ๊ฐ€ N-1์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’๊ณผ ๊ฐ™๋‹ค.
    N๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ๋Š” N-1๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๊ณ  N-3๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ์™€(=ํฌ๋„์ฃผ๊ฐ€ N-3์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’), N-1๋ฒˆ์งธ ์ž”์„ ์„ ํƒํ•˜์ง€ ์•Š๊ณ  ํฌ๋„์ฃผ๊ฐ€ N-2์ž” ์ด์—ˆ์„ ๋•Œ ๊ตฌํ•œ ์ตœ๋Œ€๊ฐ’์„ ํ™œ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.
D[N] = max(D[N-1], A[N] + A[N-1] + D[N-3],A [N] + D[N-2])

 

[์ฝ”๋“œ]

#include <iostream>
#include <vector>
using namespace std;

void DrinkGrapeJuice(int*, int);
int Max(int, int, int);

int main() {
    int num = 0;
    int grape_juice[10001] = {0,};

    cin >> num;
    for (int i = 1; i <= num; i++) {
        cin >> grape_juice[i];
    }
    DrinkGrapeJuice(grape_juice, num);

    return 0;
}

void DrinkGrapeJuice(int *grape_juice, int num) {
    int dp[10001] = {0,};

    dp[1] = grape_juice[1];
    if (num >= 2) dp[2] = dp[1] + grape_juice[2];

    for (int i = 3; i <= num; i++) {
        dp[i] = Max(dp[i - 1], dp[i - 2] + grape_juice[i], dp[i - 3] + grape_juice[i - 1] + grape_juice[i]);
    }
    cout << dp[num];
}

int Max(int a, int b, int c) {
    return a > b ? (a > c ? a : c) : (b > c ? b : c);
}
728x90