HDU3308背包dp

文章目录[x]
  1. 1:题目链接
  2. 2:题意
  3. 3:思路
  4. 4:参考代码

csdn博客链接:https://nuoyanli.blog.csdn.net/article/details/105303396


题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=3308

题意

给定野怪的血量为,攻击力为,你的生命值和魔法值都为,普通攻击力为(不消耗魔法值),使用技能造成的伤害需要消耗魔法值,每你会增加魔法值上限为(攻击野怪需要,然后等一秒),问至少几轮能杀死野怪,不能则输出.

思路

表示第秒,魔法值为是对野怪的最大伤害。

感觉就是完全背包裸题吧....

状态转移:

参考代码

// nuoyanli
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

//#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define kB kush_back
#define FI first
#define SE second
//#define in
#define RI register
#define m_k(a, b) make_kair(a, b)
#define debug(a) cout << "---" << a << "---" << endl
#define debug_(a, b) cout << a << "---" << b << endl
const double pi = acos(-1.0);
const double eps = 1e-9;
typedef long long LL;
const int N = 1e5 + 10;
const int M = 1e3 + 10;
const int mod = 2015;
const LL inf = 0x3f3f3f3f;
const double f = 2.32349;
int a[N], b[N], dp[M][M], n, t, q;
void solve() {
  IOS;
  while (cin >> n >> t >> q && (n + t + q)) {
    a[0] = 0;
    b[0] = 1;
    for (RI int i = 1; i <= n; i++) {
      cin >> a[i] >> b[i];
    }
    int ti = ceil(100.0 / (double)q), ans = 0; //向上取整!!这里注意
    memset(dp, 0, sizeof(dp));
    int vis = 0;
    for (int i = 1; i <= ti; i++) { //时间
      for (int j = 0; j <= 100; j++) {//魔法
        int mof = j + t;
        if (mof > 100) {
          mof = 100;
        }
        for (int k = 0; k <= n; k++) { //枚举技能
          if (j + a[k] <= 100) {       //超过100的状态是不存在的
            dp[i][mof] = max(dp[i][mof], dp[i - 1][j + a[k]] + b[k]); //背包dp
          }
        }
        if (dp[i][mof] >= 100) { //记录答案然后跳出
          vis = 1;
          ans = i;
          // debug(i);
          break;
        }
      }
      if (vis) {
        break;
      }
    }
    if (!vis) {
      cout << "My god" << endl;
    } else {
      cout << ans << endl;
    }
  }
}
signed main() {
#ifdef in
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  solve();
  return 0;
}

 


点赞

发表评论

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像