hdu2159

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159
题目描述:

最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能升掉这最后一级吗?

输入:

输入数据有多组,对于每组数据第一行输入n,m,k,s(0 < n,m,k,s < 100)四个正整数。分别表示还需的经验值,保留的忍耐度,怪的种数和最多的杀怪数。接下来输入k行数据。每行数据输入两个正整数a,b(0 < a,b < 20);分别表示杀掉一只这种怪xhd会得到的经验值和会减掉的忍耐度。(每种怪都有无数个)

输出:

输出升完这级还能保留的最大忍耐度,如果无法升完这级输出-1。

输入示例:

10 10 1 10
1 1
10 10 1 9
1 1
9 10 2 10
1 1
2 2

输出示例:

0
-1
1

大致思路

  • 完全背包问题,第i种怪物最多可以打V/cost[i]个,把多个i种怪物转换为V/cost[i]个消耗耐力值和获得经验不变的怪物。
  • 利用二进制进一步进行优化,可以有效减少物品数目,无论需要打倒多少只第i种怪物都可由分解后的怪物组合表示。
int temp_pow = pow(2, 0);
for (int j = 0; b*temp_pow<= m; j++) 
{
    temp_pow = int(pow(2, j));
    int temp_cost = b * temp_pow;
    int temp_value = a * temp_pow;
    monster.push_back(*(new node(temp_cost, temp_value,b)));
}
  • 题目中还有打怪数量的限制,dp为二维数组,dp[i][j]表示耐力值为i时,打怪数量为j时可获取的最大经验值
  • 状态转移方程:dp[j][0] = max(dp[j][0],dp[j - monster[i].cost][0] + monster[i].value)

代码

#include<iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
struct node
{
    int cost;
    int value;
    int s;
    node(int cost, int value, int s) { this->cost = cost; this->value = value; this->s = s; }
};
int dp[105][2];
int main()
{
    int n, m, k, s;
    while (~scanf("%d %d %d %d",&n,&m,&k,&s))
    {
        vector<node>monster;
        for (int i = 0; i < k; i++)
        {
            int a, b;
            cin >> a >> b;
            int temp_pow = pow(2, 0);
            for (int j = 0; b*temp_pow<= m; j++) 
            {
                temp_pow = int(pow(2, j));
                int temp_cost = b * temp_pow;
                int temp_value = a * temp_pow;
                monster.push_back(*(new node(temp_cost, temp_value,b)));
            }
        }
        memset(dp, 0, sizeof(dp));
        int len = monster.size();
        for (int i = 0; i < len; i++)
        {
            for (int j = m; j >= monster[i].cost; j--)
            {
                int temp_a = dp[j][0];
                int temp_b = dp[j - monster[i].cost][0] + monster[i].value;
                if (temp_b > temp_a) dp[j][1]= dp[j - monster[i].cost][1]+monster[i].cost/monster[i].s;
                dp[j][0] = max(temp_a, temp_b);
            }
        }
        int flag = 0;
        for (int i = 0; i <= m; i++)
        {
            if (dp[i][0] >= n && dp[i][1] <= s)
            {
                cout << m - i << endl;
                flag = 1;
                break;
            }
        }
        if(!flag) cout << -1 << endl;

    }
}

转载于:https://www.cnblogs.com/l-h-x/p/8490867.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值