CF-C. Dima and Salad

题目链接:https://codeforces.com/problemset/problem/366/C

题意:给定n个物品,每个物品都有一个美味值和卡路里值,从其中选出若干个使得成立,求成立的方案中,美味值的最大值。

分析:先把式子化简得(a[i] - k * b[i]), 设f[i][j]为前i个物品中获得美味值与卡路里值得差值为j得美味值max,显然f[i][j] = max(f[i - 1][j], f[i - 1][j - (a[i] - k * b[i])] + a[i]);

值得一提的是j - (a[i] - k * b[i])可能为负数,所以我们需要让其整体往右+100000即可,目标状态则为f[n][100000].

代码:

#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define x first
#define y second
#define endl '\n'

using namespace std;
typedef pair<int, int> PII;
const int N = 1e3 + 10;
const int M = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const double INFF = 0x7f7f7f7f7f7f7f7f;
const int mod = 1e9 + 7;
int n, k;
int a[N], b[N];
int f[105][N * 200];
signed main()
{
    ios;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        cin >> b[i];

    memset(f, -0x3f, sizeof(f));
    f[0][100000] = 0;
    
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= 200000; j++)
        {
            if(j - (a[i] - k * b[i]) >= 0)
            f[i][j] = max(f[i - 1][j], f[i - 1][j - (a[i] - k * b[i])] + a[i]);
        }
    }
    int ans = f[n][100000];
    if (ans == 0)
        ans = -1;
    cout << ans << endl;
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值