中文题目就不用解释了 就是裸的二维完全背包
d[i][j]表示消耗i忍耐杀j个怪最多可获得的经验 然后就用完全背包来做了 二维背包背包不过是多了一重循环
<span style="font-family:Arial Black;">#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 105; int main() { //还需的经验值n,保留的忍耐度m,怪的种数k,最多的杀怪数s int n, m, k, s, d[N][N], exp[N], cos[N]; while (~scanf ("%d%d%d%d", &n, &m, &k, &s)) { memset (d, 0, sizeof (d)); for (int i = 1; i <= k; ++i) scanf ("%d%d", &exp[i], &cos[i]); for (int u = 1; u <= k; ++u) for (int i = cos[u]; i <= m; ++i) for (int j = 1; j <= s; ++j) d[i][j] = max (d[i][j], d[i - cos[u]][j - 1] + exp[u]); int i; for (i = 1; i <= m; ++i) if (d[i][s] >= n) break; if (i <= m) printf ("%d\n", m - i); else printf ("-1\n"); } return 0; } </span>
10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
0 -1 1
HDU 2159 FATE(二维完全背包),布布扣,bubuko.com
原文地址:http://blog.csdn.net/iooden/article/details/38466989