2021 山东省赛 H . Adventurer’s Guild(01背包)

简介: 2021 山东省赛 H . Adventurer’s Guild(01背包)

描述:

Yuna traveled into the fantasy world. The gods of this world gave her a powerful set of equipment so that she could defeat many fifierce monsters. However, she had limited health points and stamina, and she could not kill a large number of monsters.


Adventurer’s guild would release n monster crusade missions, such as black python and wild wolf. Completing the i-th mission would consume Yuna hi health points and si stamina, and then she would get wi gold coins.


In the beginning, Yuna had H health points and S stamina. When her health points were dropped to less than or equal to 0, she would die. However, when her stamina was dropped to less than 0, she would not give up, and then the overdrawn stamina would be reduced from health points. For example, her health points would be reduced by 3, when her stamina dropped to −3, and then her stamina would be reset to 0. If her health points can not afffford the overdrawn stamina, she would also die.


As a friend of Yuna, can you help her choose some missions to complete to get the maximum number of gold coins? Make sure Yuna does not die, or you will be very sad.


输入:

The first line contains three integers n,H,S (1≤n≤1000, 1≤H≤300, 0≤S≤300).


The next n lines describe all the monster crusade missions, where the i-th line contains three integers hi,si,wi (0≤hi,si≤300, 1≤wi≤109).


输出:

Print one integer – the maximum number of gold coins that Yuna could get.


大意:

异世界的尤娜有 生命值和耐力值,打怪消耗生命值和耐力值,获得金币,当生命值小于 0 即死亡,耐力值小于 0时耐力值重置为 0,消耗生命值。


思路:

一个01背包问题,直接写了


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ll maxx = 1e18;
const int N = 1e5+100;
const int p = 1e4;
const double eps = 1e-8;
ll dp[301][301];// j h    k  s 
int n,h,s;
int hs[1001],ss[1001],wss[1001];
int main()
{
  cin>>n>>h>>s;
  for(int i=1;i<=n;i++)
  {
    cin>>hs[i]>>ss[i]>>wss[i];
  }//hs健康,ss耐力,wss金币
  for(int i=1;i<=n;i++)
  {
    for(int j=h;j>=0;j--)
    {
      for(int k=s;k>=0;k--)
      {
        if(j>hs[i]&&k>=ss[i])//当生命与耐力都足够时
          dp[j][k]=max(dp[j][k],dp[j-hs[i]][k-ss[i]]+wss[i]);
        else
        if(k<ss[i]&&j+k>(hs[i]+ss[i]))
          dp[j][k]=max(dp[j][k],dp[k+j-(hs[i]+ss[i])][0]+wss[i]);//当耐力不够但是生命+耐力足够时,注意此时耐力清零
      }
    }
  }
  cout<<dp[h][s];
  return 0;
}


反思:


当耐力不够的时候,应该是生命值去抵消多出的耐力消耗值,而且此时耐力值清零,注意这两个易错点


目录
相关文章
|
7月前
【天梯赛】L1-095 分寝室
输出的方案对应女生都是 24/4=6 人间、男生都是 60/6=10 人间,人数差为 4。满足前三项要求的分配方案还有两种,即女生 6 间(都是 4 人间)、男生 4 间(都是 15 人间);同时,每间女寝人数必须都一样,每间男寝人数必须都一样,也就是女生总人数对女寝数取模为0,男生总人数对男寝数取模为0。输入在一行中给出 3 个正整数 n0​、n1​、n,分别对应女生人数、男生人数、寝室数。按题意模拟,因为知道总寝室数为n,所以可以从1~n-1暴力枚举女寝 i 的数量,那么男寝的数量则为 c-i。
111 6
|
算法
山东第一届省赛 Greatest Number(优雅暴力+二分)
山东第一届省赛 Greatest Number(优雅暴力+二分)
88 1
|
测试技术 C++
【PTA天梯赛】L1-001 L1-002 L1-003 L-004 L-005 L-006 L-007 L-008 L-009 L1-010 c++
【PTA天梯赛】L1-001 L1-002 L1-003 L-004 L-005 L-006 L-007 L-008 L-009 L1-010 c++
238 1
【PTA天梯赛】L1-011 —— L1-020 c++ 题解
【PTA天梯赛】L1-011 —— L1-020 c++ 题解
438 0
|
存储 人工智能 BI
P8597 [蓝桥杯 2013 省 B] 翻硬币个人思考总结+第五届传智杯ABC 初赛题解
桌上放着排成一排的若干硬币。我们用 `*` 表示正面,用 `o` 表示反面(是小写字母,不是零),比如可能情形是 `**oo***oooo`,如果同时翻转左边的两个硬币,则变为 `oooo***oooo`。现在小明的问题是:如果已知了初始状态和要达到的目标状态,每次只能同时翻转相邻的两个硬币,那么对特定的局面,最少要翻动多少次呢?
193 0
L1-079 天梯赛的善良 (20 分)
L1-079 天梯赛的善良 (20 分)
230 0
|
算法 BI vr&ar
【算法题解】2022河南萌新联赛第(三)场:河南大学
【算法题解】2022河南萌新联赛第(三)场:河南大学
【算法题解】2022河南萌新联赛第(三)场:河南大学
|
并行计算
2019年第10届山东省赛 L . Median (floyd 拓扑排序)
2019年第10届山东省赛 L . Median (floyd 拓扑排序)
114 0