题意:
有 n 种存款,分别对应 n 种利息。问经过 y 年,所得到的最大资产为多少。
思路:
1. 因为存款的种类都是 1000 的倍数,所以可以对其除 1000,以缩小 dp 数组的规模。
2. 由于存款的每一种是无限制的,所以用到了完全背包的策略。
3. 每一年更新下背包的容量,然后根据年数重复背包策略。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 140000;
int dp[maxn];
int main()
{
int cases;
scanf("%d", &cases);
while (cases--)
{
int vol, y, n;
int bond[12], value[12];
scanf("%d %d %d", &vol, &y, &n);
for (int i = 0; i < n; ++i)
{
scanf("%d %d", &bond[i], &value[i]);
bond[i] /= 1000;
}
int ret = vol;
for (int i = 0; i < y; ++i)
{
vol = ret / 1000;
for (int j = 0; j < n; ++j)
{
for (int w = bond[j]; w <= vol; ++w)
dp[w] = max(dp[w], dp[w - bond[j]] + value[j]);
}
ret += dp[vol];
memset(dp, 0, sizeof(dp));
}
printf("%d\n", ret);
}
return 0;
}