题目大意:最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能升掉这最后一级吗?
解题思路:贪心,肯定是经验高且消耗的忍耐度最低的怪先杀,按此排序
接下来就是01背包的问题了
用dp[i][j]表示获得经验为i,还能杀j个怪时残留下来的最大忍耐度
转移方程就是
dp[i + experience][j - 1] = max(dp[i + experience][j - 1], dp[i][j] - cost)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110 * 2;
const int INF = 0x3f3f3f3f;
struct Monster{
int val, cost;
}mon[N];
int n, m, k, s;
int dp[N][N];
bool cmp(const Monster a, const Monster b) {
return 1.0 * a.val / a.cost > 1.0 * b.val / b.cost;
}
void init() {
for (int i = 0; i < k; i++)
scanf("%d%d", &mon[i].val, &mon[i].cost);
sort(mon, mon + k, cmp);
}
void solve() {
memset(dp, -1, sizeof(dp));
for (int i = s; i > 0; i--)
dp[0][i] = m;
for (int i = 0; i < k; i++)
for (int j = 0; j <= n && j + mon[i].val < N; j++)
for (int l = s; l > 0; l--)
if (dp[j][l] > 0)
dp[j + mon[i].val][l - 1] = max(dp[j + mon[i].val][l - 1], dp[j][l] - mon[i].cost);
int ans = -INF;
for (int i = n; i < N; i++)
for (int j = 0; j <= s; j++)
ans = max(ans, dp[i][j]);
printf("%d\n", ans);
}
int main() {
while (scanf("%d%d%d%d", &n, &m, &k, &s) != EOF) {
init();
solve();
}
return 0;
}