[λ¬Έμ λ§ν¬]
https://www.acmicpc.net/problem/2156
[νμ΄μμ€]
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'μ μ μ₯κ³΅κ° νμ!!
'Algoritm > Quiz-Solutions' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Programmers/μ½λ©ν μ€νΈμ°μ΅/μ λ ¬] kλ²μ§Έ μ (0) | 2020.08.30 |
---|---|
[Leetcode 1] two Sum (0) | 2020.08.02 |
[λ°±μ€ 1260] DFSμ BFS (Java) - DFS,BFS (0) | 2020.05.07 |
[λ°±μ€ 9012] κ΄νΈ(Java) - Stack (0) | 2020.05.04 |
[λ°±μ€ 10854] ν (JAVA) - Queue (0) | 2020.05.03 |
λκΈ