λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Algoritm/Quiz-Solutions

[λ°±μ€€ 2156] 포도주 μ‹œμ‹ (Java) - DP

by πŸ’œautumn 2020. 5. 7.

[문제링크]

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

 

2156번: 포도주 μ‹œμ‹

νš¨μ£ΌλŠ” 포도주 μ‹œμ‹νšŒμ— κ°”λ‹€. κ·Έ 곳에 κ°”λ”λ‹ˆ, ν…Œμ΄λΈ” μœ„μ— λ‹€μ–‘ν•œ 포도주가 λ“€μ–΄μžˆλŠ” 포도주 μž”μ΄ 일렬둜 놓여 μžˆμ—ˆλ‹€. νš¨μ£ΌλŠ” 포도주 μ‹œμ‹μ„ ν•˜λ €κ³  ν•˜λŠ”λ°, μ—¬κΈ°μ—λŠ” λ‹€μŒκ³Ό 같은 두 가지 κ·œμΉ™μ΄ μžˆλ‹€. 포도주 μž”μ„ μ„ νƒν•˜λ©΄ κ·Έ μž”μ— λ“€μ–΄μžˆλŠ” ν¬λ„μ£ΌλŠ” λͺ¨λ‘ λ§ˆμ…”μ•Ό ν•˜κ³ , λ§ˆμ‹  ν›„μ—λŠ” μ›λž˜ μœ„μΉ˜μ— λ‹€μ‹œ 놓아야 ν•œλ‹€. μ—°μ†μœΌλ‘œ 놓여 μžˆλŠ” 3μž”μ„ λͺ¨λ‘ λ§ˆμ‹€ μˆ˜λŠ” μ—†λ‹€. νš¨μ£ΌλŠ” 될 수 μžˆλŠ” λŒ€λ‘œ λ§Žμ€ μ–‘μ˜ 포도주λ₯Ό 맛보기 μœ„ν•΄μ„œ μ–΄λ–€ 포도주 μž”μ„ 선택해야 할지 κ³ 

www.acmicpc.net

[ν’€μ΄μ†ŒμŠ€]

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
package algorithm;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Dp2156 {
    public static int[] glassWine; // κ° μž”에 λ‹΄κΈ΄ ν¬λ„μ£Ό μ–‘
    public static int[] totalWine; // i번째 κΉŒμ§€ λ¨Ήμ—ˆμ„λ•Œμ˜ μ΅œλŒ€ ν¬λ„μ£Ό μ–‘ 
    public static int maxWine;
    
    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        
        int glassCount=Integer.parseInt(br.readLine());
        
        // μ ν™”μ‹μ—μ„œ μ•žμ— μ„Έκ°œκΉŒμ§€ ν•„μš”ν•˜κΈ°λ•Œλ¬Έμ— 0, 0, 0 μž‘κ³  ν™œμš©
        //   => λ”°λΌμ„œ +3ν•΄μ„œ λ°°μ—΄ μ„ μ–Έ
        glassWine=new int[glassCount+3];
        totalWine=new int[glassCount+3];
        
        for(int i=0; i<glassCount; i++) {
            // 3λ²ˆμ§ΈλΆ€ν„° μž…λ ₯값을 ν¬λ„μ£Ό μ–‘ λ°°μ—΄μ— μ €μž₯ (μ•žμ— 3μΉΈ λΉ„μš°κΈ°)
            glassWine[3+i]=Integer.parseInt(br.readLine());
        }
        
        // μ ν™”식 μž‘μ„±ν•˜μ—¬ κ²°κ³Όκ°’ λ„μΆœ
        for(int i=3;i<glassWine.length;i++) {
            int drink0=totalWine[i-1];
            int drink1=totalWine[i-2]+glassWine[i];
            int drink2=totalWine[i-3]+glassWine[i-1]+glassWine[i];
            // 3가지 κ²½μš° μ€‘ μ΅œλŒ€κ°’
            maxWine= Math.max(drink0, Math.max(drink1, drink2));
            // μ΅œλŒ€κ°’을 μ΅œλŒ€κ°’배열에 λ„£κΈ°!
            totalWine[i]=maxWine;
        }
        System.out.println(maxWine);
    }
 
}
 
cs

 

 

 

[λŠλ‚€μ ]

    - DPμ—μ„œ κ°€μž₯ μ€‘μš”ν•œ 것은 점화식

    - μ ν™”μ‹μ—μ„œ μ‚¬μš©λ  λ²”μœ„(n의 μ—°κ΄€λ²”μœ„)만큼의 '0'의 μ €μž₯곡간 ν•„μš”!!

λŒ“κΈ€