티스토리 뷰

PS

[백준] 1535번 안녕 C++

Eastplanet 2022. 6. 24. 19:14

문제 출처: https://www.acmicpc.net/problem/1535

 

1535번: 안녕

첫째 줄에 사람의 수 N(≤ 20)이 들어온다. 둘째 줄에는 각각의 사람에게 인사를 할 때, 잃는 체력이 1번 사람부터 순서대로 들어오고, 셋째 줄에는 각각의 사람에게 인사를 할 때, 얻는 기쁨이 1번

www.acmicpc.net

[체력][현재 사람]인덱스를 사용하여 top-down방식으로 작성하여 풀었음.

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include <string>

using namespace std;


int N;
int happy[20];
int hp[20];
int dp[101][20];

int rec(int now, int nowHp) {
    if (now == N) {
        if (dp[nowHp][now] == -1)return 0;
        else return dp[nowHp][N - 1];
    }
    if (dp[nowHp][now] != -1)return dp[nowHp][now];

    int val = rec(now + 1, nowHp);
    if (nowHp - hp[now] > 0) {
        val = max(val,rec(now+1, nowHp - hp[now])+happy[now]);
    }

    dp[nowHp][now] = val;
    return val;

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> N;
    
    fill(&dp[0][0], &dp[100][20], -1);

    for (int i = 0; i < N; i++) {
        cin >> hp[i];
    }
    for (int i = 0; i < N; i++) {
        cin >> happy[i];
    }

    int val = rec(0, 100);

    if (val == -1)cout << "0";
    else cout << val;

}

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/01   »
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
글 보관함