欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDOJ2159 FATE #基础DP 二维费用背包#

程序员文章站 2022-06-19 13:34:22
...

FATE

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23510    Accepted Submission(s): 10843

Problem Description
 
最近xhd正在玩一款叫做FATE的游戏,为了得到*装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能升掉这最后一级吗?
 
Input
 
输入数据有多组,对于每组数据第一行输入n,m,k,s(0 < n,m,k,s < 100)四个正整数。分别表示还需的经验值,保留的忍耐度,怪的种数和最多的杀怪数。接下来输入k行数据。每行数据输入两个正整数a,b(0 < a,b < 20);分别表示杀掉一只这种怪xhd会得到的经验值和会减掉的忍耐度。(每种怪都有无数个)
 
Output
 
输出升完这级还能保留的最大忍耐度,如果无法升完这级输出-1。
 
Sample Input
 
10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
 
Sample Output
 
0 -1 1
 
Author
 
Xhd
 
Source
 
 

Recommend

linle   |   We have carefully selected several similar problems for you:  2602 1203 1171 2955 2844 
 
Solution
 
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1e2 + 10;
int dp[maxn][maxn];
struct item { int a, b; } items[maxn];

inline const int read()
{
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

int main()
{
    int n, m, k, s;
    while (~scanf("%d%d%d%d", &n, &m, &k, &s))
    {
        memset(dp, 0, sizeof(dp));
        for (int i = 1; i <= k; i++)
        {
            items[i].a = read();
            items[i].b = read();
        }
        for (int i = 1; i <= k; i++)
            for (int j = items[i].b; j <= m; j++)
                for (int k = 1; k <= s; k++)
                    dp[j][k] = max(dp[j][k], dp[j - items[i].b][k - 1] + items[i].a);
        int res = m + 1;
        for (int i = 0; i <= m; i++)
            for (int j = 0; j <= s; j++)
                if (dp[i][j] >= n)
                    res = min(res, i);
        printf("%d\n", m - res);
    }
    return 0;
}

 

相关标签: Online Judge