문제) 백준 - DP(동적 계획법) - 앱
https://www.acmicpc.net/problem/7579
7579번: 앱
입력은 3줄로 이루어져 있다. 첫 줄에는 정수 N과 M이 공백문자로 구분되어 주어지며, 둘째 줄과 셋째 줄에는 각각 N개의 정수가 공백문자로 구분되어 주어진다. 둘째 줄의 N개의 정수는 현재 활
www.acmicpc.net
메모리를 memoization을 진행하면 메모리 초과로 터진다.
dp[idx][Cost]: Cost로 idx번째 앱에서 얻을 수 있는 최대 메모리를 계산한다. Cost를 0부터 증가시켜 M보다 클 경우 break 하여 출력한다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int solve(int idx, int C) { | |
if (idx == N) return 0; | |
int& ret = cache[idx][C]; | |
if (ret != -1) return ret; | |
//활성화 | |
ret = solve(idx + 1, C); | |
//비활성화 | |
if (C >= cost[idx]) | |
ret = max(ret, solve(idx + 1, C - cost[idx]) + memory[idx]); | |
return ret; | |
} |
Full Code)
https://github.com/Chocochip101/BOJ_Solution/blob/main/Problem%205000~9999/7579_%EC%95%B1.cpp
GitHub - Chocochip101/BOJ_Solution
Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 5014번 - 스타트링크 (C++) 문제 및 풀이 (0) | 2021.11.09 |
---|---|
[백준] 9372번 - 상근이의 여행 (C++) 문제 및 풀이 (0) | 2021.11.07 |
[백준] 12852번 - 1로 만들기 2 (C++) 문제 및 풀이 (0) | 2021.11.06 |
[백준] 11060번 - 점프 점프 (C++) 문제 및 풀이 (0) | 2021.11.04 |
[백준] 9507번 - Generations of Tribbles (C++) 문제 및 풀이 (0) | 2021.11.03 |
댓글