HDU3308揹包dp

個人博客鏈接:https://blog.nuoyanli.com/2020/04/03/hdu3308/

題目鏈接

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

題意

給定野怪的血量爲100100,攻擊力爲qq,你的生命值和魔法值都爲100100,普通攻擊力爲11(不消耗魔法值),使用技能造成的傷害需要消耗魔法值,每2s2s你會增加tt魔法值上限爲100100(攻擊野怪需要1s1s,然後等一秒),問至少幾輪能殺死野怪,不能則輸出My,godMy,god.

思路

dp[i][j]:dp[i][j]:表示第ii秒,魔法值爲jj是對野怪的最大傷害。

感覺就是完全揹包裸題吧…

狀態轉移:

dp[i][j]=max(dp[i][j],dp[i1][ja[k]]+b[k])dp[i][j]= max( dp[i][j], dp[i-1][j-a[k]]+b[k] )

參考代碼

// 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章