문제출처 : baekjoon online judge

 

14501번: 퇴사

첫째 줄에 백준이가 얻을 수 있는 최대 이익을 출력한다.

www.acmicpc.net

이 문제는 전형적인 다이나믹프로그래밍 문제입니다.

각 날짜마다 일했을 때, 구할 수 있는 최대 이윤과 그 날 일하지 않고 다음 날 일했을 때 최대 이윤을 비교하는 방식입니다.

각 날짜마다 걸리는 시간을 고려해서, 일 할 수 없으면 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
47
48
#include<iostream>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
int cache[20];
 
int t[20];
int p[20];
 
int n;
 
int dp(int x) {
    if (x > n)
        return 0;
 
    int &ret = cache[x];
 
    if (ret != -1)
        return ret;
 
    if (x + t[x] - 1 > n)
        return ret = max(0, dp(x + 1));
 
 
    return ret = max(dp(x + 1), p[x] + dp(x + t[x]));
}
 
int main() {
    cin >> n;
 
    for (int i = 1; i <= n; i++) {
        cin >> t[i] >> p[i];
    }
 
    memset(cache, -1sizeof(cache));
 
    dp(1);
 
    int answer = 0;
 
    for (int i = 1; i <= n; i++) {
        answer = max(answer, cache[i]);
    }
 
    cout << answer << endl;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter

 

의견이 있으시거나 수정해야할 부분이 있으면 편하게 공유 부탁드립니다!

+ Recent posts