다이나믹 프로그래밍 한접시

[백준] 2156번: 포도주 시식 [C#]

NaZZU 2024. 5. 1. 22:45

https://www.acmicpc.net/board/view/139143

이 링크를 타고 들어가면 아주 훌륭한 예제들을 볼 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _2156_boj boj = new _2156_boj();
 
            boj.boj_2156();
 
        }
    }
        internal class _2156_boj
    {
        public void boj_2156()
        {
            int n = int.Parse(Console.ReadLine());
 
            int[] arr = new int[n + 1];
            for (int i =1; i <= n; i++)
            {
                arr[i] = int.Parse(Console.ReadLine());
            }
 
            int[] dp = new int[n + 1];
            dp[0= 0;
            dp[1= arr[1];
            if (n >= 2)
                dp[2= arr[1+ arr[2];
 
            for (int i = 3; i <= n; i++)
            {
                dp[i] = Math.Max(Math.Max(arr[i] + dp[i - 2], arr[i] + dp[i - 3+ arr[i - 1]), dp[i - 1]);
 
            }
            Console.WriteLine(dp.Max());
        }
    }
}
 
cs

 

정말 오랜만에 풀어보는 계단문제였다.

dp[i]의 값을 판별하는 방법 중, dp[i - 1]과도 대소비교를 해줘야 하는 걸 떠올리지 못해서 답을 얻는데 살짝 애를 먹었다.