题意:给你一笔钱,给你一个利润单,每年你可以选择去买这里的单子里面的产品,不同的产品可以赚不同的利润,每种产品可以买无数个(只要你有钱)。问n年以后,你的手里最多有多少钱。
思路:这题有点啰嗦,读懂题目以后,发现是很裸的完全背包(只不过在外面套一个循环而已)。相当于是每年都进行一次完全背包计算一下最优方案。注意它提示钱数是1000的倍数。
反思:这题可以提升一下阅读能力,还有就是再打一遍完全背包。
代码:
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <string.h>
#include <math.h>
#include <sstream>
using namespace std;
const int maxn=1e5+9;
int dp[maxn];
int v[maxn],h[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T;
scanf("%d",&T);
for(int i=0;i<T;i++)
{
int w,t;
scanf("%d%d",&w,&t);
int n;
scanf("%d",&n);
for(int j=0;j<n;j++)
{
scanf("%d%d",&v[j],&h[j]);
}
for(int k=0;k<t;k++)
{
memset(dp,0,sizeof(dp));
for(int x=0;x<n;x++)
{
for(int y=v[x]/1000;y<=w/1000;y++)
{
dp[y]=max(dp[y],dp[y-v[x]/1000]+h[x]);
}
}
w+=dp[w/1000];
}
printf("%d\n",w);
}
return 0;
}