HDU3308背包dp

个人博客链接:https://blog.nuoyanli.com/2020/04/03/hdu3308/

题目链接

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

题意

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

思路

d p [ i ] [ j ] : dp[i][j]: dp[i][j]:表示第 i i i秒,魔法值为 j j j是对野怪的最大伤害。

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

状态转移:

d p [ i ] [ j ] = m a x ( d p [ i ] [ j ] , d p [ i − 1 ] [ j − a [ k ] ] + b [ k ] ) dp[i][j]= max( dp[i][j], dp[i-1][j-a[k]]+b[k] ) dp[i][j]=max(dp[i][j],dp[i1][ja[k]]+b[k])

参考代码

// nuoyanli
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//#include 
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;
}

你可能感兴趣的:(ACM,#,DP,#,做题心得)