10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
0 -11
思路:二维数组,把忍耐值当成背包容积,dp存储经验值,另一维代表打怪的个数,达到经验值时跳出,判断忍耐值。
代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int m,n,k,s; int x,y,z; int a[200],b[200],dp[200][200]; while(~scanf("%d%d%d%d",&n,&m,&k,&s)) { for(x=1; x<=k; x++) scanf("%d%d",&a[x],&b[x]); memset(dp,0,sizeof(dp)); for(x=1; x<=m; x++) { for(y=1; y<=k; y++) { for(z=1; z<=s; z++) { int cas=1;//判断是这种怪要打多少种 while(cas*b[y]<=x&&cas<=z) { dp[x][z]=max(dp[x][z],dp[x-cas*b[y]][z-cas]+cas*a[y]); cas++; } } } if(dp[x][s]>=n)//如果你到达经验值 { break; } } if(x>m) printf("-1\n"); else printf("%d\n",m-x); } return 0; }