사용 알고리즘 : DP
dp배열 설정 (boolean)
dp[i][j]
[i번째 곡으로][볼륨 j를 연주 할 수 있는지]
주의 할 점 : 볼륨 0도 연주를 할 수 있다고 친다.
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
|
public class Main {
static int n,s,m;
static boolean dp[][];
static int vol[];
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] t = br.readLine().split(" ");
n = Integer.parseInt(t[0]); // 곡 개수
s = Integer.parseInt(t[1]); // 시작볼륨
m = Integer.parseInt(t[2]); // 최대볼륨
vol = new int[n+1];
String[] tt = br.readLine().split(" ");
for(int i=1; i<=n; i++) {
vol[i] = Integer.parseInt(tt[i-1]);
}
dp = new boolean[n+1][m+1];
dp[0][s]=true;
for(int i=1; i<=n; i++) {
for(int j=0; j<=m; j++) {
if(!dp[i-1][j]) {
continue;
} // 전단계가 성립이 안되면 계속 할 이유가 없다.
if(j+vol[i]<=m) {
dp[i][j+vol[i]]=true;
}
if(j-vol[i]>=0) {
dp[i][j-vol[i]] = true;
}
}
}
for(int i=m; i>=0; i--) {
if(dp[n][i]) {
System.out.println(i);
System.exit(0);
}
}
System.out.println(-1);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
cs |
'알고리즘' 카테고리의 다른 글
[백준 3568] iSharp -JAVA //le_effort// (0) | 2020.03.11 |
---|---|
[백준 5557] 1학년 -JAVA // le_effort// (0) | 2020.03.11 |
[백준 12865] 평범한 배낭 - JAVA // le_effort// (0) | 2020.03.10 |
[백준 11058] 크리보드 -JAVA // le_effort// (0) | 2020.03.10 |
[백준 2293] 동전 1 -JAVA //le_effort// (0) | 2020.03.10 |