简单的背包——01背包(10)

简单的背包——01背包(10)

题目来源:洛谷 P1802 5倍经验日

原题

共十个测试点

题解

WA代码
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1e3 + 5;
int n, x;
int lose, win, use;
ll f[maxn];

int main()
{
    memset(f, 0, sizeof(f));
    scanf("%d%d", &n, &x);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d%d%d", &lose, &win, &use);
        for (int j = x; j >= 1; --j)
        {
            if (j >= use)
            {
                f[j] = max(f[j] + lose, f[j - use] + win);
            }
            else
            {
                f[j] = f[j] + lose;
            }
        }
    }
    printf("%lld", 5 * f[x]);
    return 0;
}
错误分析:

j 可以等于 0 ,当 j 等于 0 时,可以选择一直输。

测试点通过情况:

#1, #2, #4 ~ #6, #8 WA

AC代码
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 1e3 + 5;
int n, x;
int lose, win, use;
ll f[maxn];

int main()
{
    memset(f, 0, sizeof(f));
    scanf("%d%d", &n, &x);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d%d%d", &lose, &win, &use);
        for (int j = x; j >= 0; --j)
        {
            // 当 j >= use 时,f[j] 可以选择赢和输;
            if (j >= use)
            {
                f[j] = max(f[j], max(f[j] + lose, f[j - use] + win));
            }
            // 当 j <= use 时,f[j] 只能选择输;
            else
            {
                f[j] = max(f[j], f[j] + lose);
            }
        }
    }
    printf("%lld", 5 * f[x]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值