10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
0 -1 1
显然把忍耐度和杀怪数为背包容量,经验为价值做二维背包:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAX = 105; int n, m, k, s; int dp[MAX][MAX], v[MAX], p[MAX]; int main() { while(scanf("%d %d %d %d", &n, &m, &k, &s) != EOF) { memset(dp, 0, sizeof(dp)); for(int i = 0;i < k; ++i) scanf("%d %d", &v[i], &p[i]); for(int i = 0;i < k; ++i) { for(int j = p[i];j <= m; ++j) { for(int l = 1;l <= s; ++l) { dp[j][l] = max(dp[j][l], dp[j-p[i]][l-1]+v[i]); } } } if(dp[m][s] < n) cout << -1 << endl;//不足以升级 else { int used = m; for(int i = 0;i <= m; ++i) for(int j = 0;j <= s; ++j) { if(dp[i][j] >= n && i < used) used = i; } cout << m-used << endl; } } return 0; }