반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. Dynamic Programming

2. 점화식 세우는 방법


다이나믹 프로그래밍으로 문제를 해결 할 수 있다.


dp[i][j] :: i번째에서 j의 볼륨을 가질 수 있다면 true, 아니면 false


n 제한은 100, m 제한은 1000이기에 이중 for문으로 충분히 시간 해결이 가능하다.





소스 코드 : 


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
#include <iostream>
#include <cstdio>
 
using namespace std;
 
bool dp[102][1002];
int arr[102];
 
int main()
{
    int n, s, m;
    scanf("%d %d %d"&n, &s, &m);
 
    for (int i = 1; i <= n; i++)
        scanf("%d"&arr[i]);
 
    dp[0][s] = true;
 
    for (int i = 0; i < n; i++)
        for(int vol = 0; vol <= m; vol++)
            if (dp[i][vol])
            {
                if (vol + arr[i + 1<= m)
                    dp[i + 1][vol + arr[i + 1]] = true;
                if (vol - arr[i + 1>= 0)
                    dp[i + 1][vol - arr[i + 1]] = true;
            }
 
    int ans = -1;
    for (int vol = 0; vol <= m; vol++)
        if (dp[n][vol])
            ans = vol;
 
    printf("%d", ans);
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus



반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[1893번] 시저 암호  (0) 2017.12.11
[11585번] 속타는 저녁 메뉴  (0) 2017.12.11
[3640번] 제독  (0) 2017.12.09
[11407번] 책 구매하기  (0) 2017.12.08
[8992번] 집기 게임  (0) 2017.12.07