解题思路:
总共有六个参数,n(还需的经验值),m(保留的耐久度),k(怪的种类),s(最大杀怪数),a(所得经验值),b(损耗的耐久度)。
这道题与01背包很类似,每种有怪有选与不选两种,但是由于怪物是无限的,所以在选之中又有选几只的差别。这种类型对应着其实是多重背包问题。
每一种状态可以用三个参数d(i,j,l)来表示,其中i:第几种怪物;j:剩余耐久度;l:剩余杀怪数。
这里其实可以省略一维i,即采用滚动数组的形式。
ps:因为第i-1层的结果是服务于第i层的,一旦被使用过,其实第i-1层就没有用了。
状态转移方程:
d[j][l] = max(d[j][l],d[j-p*a[i]][l-p]+p*b[i]);
(p为杀多少只 ; j >= p*a[i]; p <= l)
具体代码:
#include <set>
#include <numeric>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
#include <sstream>
#include <map>
#include <functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define REP(idx1,num1) for(int idx1=0;idx1<(num1);idx1++)
#define pb push_back
#define empb emplace_back
#define mp make_pair
#define mem(s) memset(s,0,sizeof(s));
const double EPS = 1e-6;
const int maxn = 100+10;
int a[maxn],b[maxn];
int d[maxn][maxn];//i:耐久度,j:杀怪数
int main(){
int n,m,k,s;
while(~scanf("%d%d%d%d",&n,&m,&k,&s)){
for(int i = 1; i <= k; ++i)
scanf("%d%d",&a[i],&b[i]);
mem(d);
for(int i = 1; i <= k; ++i)
for(int l = s; l >= 0; --l){//可杀数
for(int p = l; p >= 0; --p){//实际杀怪数
for(int j = m; j >= p*b[i]; --j){
d[j][l] = max(d[j][l],d[j-p*b[i]][l-p]+p*a[i]);
}
}
}
int ans = 0;
bool flag = false;
//最小耗损
for(int i = m; i >= 0; --i)
for(int j = 0; j <= s; ++j){
if(d[i][j] >= n){
ans =max(ans, m-i);
flag = true;
}
}
if(flag)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}