문 제
소 스 코 드
#include <iostream>
#include <algorithm>
constexpr int MAX{ 100 };
constexpr int COSTMAX{ 10'000 };
typedef std::pair<int, int> App;
App apps[MAX]{std::make_pair(0, 0),};
int dp[COSTMAX]{ 0, };
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
int N, M;
std::cin >> N >> M;
int maxCost{ 0 };
for (int i = 0; i < N; ++i) std::cin >> apps[i].first;
for (int i = 0; i < N; ++i) {
std::cin >> apps[i].second;
maxCost += apps[i].second;
}
for (int i = 0; i < N; ++i) {
for (int j = maxCost; j >= apps[i].second; --j) {
dp[j] = std::max(dp[j], dp[j - apps[i].second] + apps[i].first);
}
}
for (int i = 0; i < maxCost; ++i) {
if (M <= dp[i]) {
std::cout << i << '\n';
break;
}
}
return 0;
}
풀 이
더보기

평범한 배낭과 비슷한 유형의 문제이다.

누적된 결과 이기 때문에,
dp[cost] = Max(dp[cost], dp[cost - cost[i]]] + memory[i])
를 통하여 누적된 값을 찾아 구할 수 있다.
for문을 maxCost ~ cost[i] 까지 -- 동작을 해야 이전 동작에 상관없이 올바른 결과를 찾을 수 있다.
출 력 값
문 제 출 처
문제 링크 : https://www.acmicpc.net/problem/7579
7579번: 앱
입력은 3줄로 이루어져 있다. 첫 줄에는 정수 N과 M이 공백문자로 구분되어 주어지며, 둘째 줄과 셋째 줄에는 각각 N개의 정수가 공백문자로 구분되어 주어진다. 둘째 줄의 N개의 정수는 현재 활��
www.acmicpc.net
'[ ALGORITHM ] > [ 백 준 ]' 카테고리의 다른 글
[BAEKJOON] 1260번 : DFS와 BFS (0) | 2020.08.28 |
---|---|
[BAEKJOON] 10942번 : 팰린드롬? (0) | 2020.08.27 |
[BAEKJOON] 1520번 : 내리막 길 * (0) | 2020.08.25 |
[BAEKJOON] 11049번 : 행렬 곱셈 순서* (0) | 2020.08.24 |
[BAEKJOON] 11066번 : 파일 합치기* (0) | 2020.08.23 |