[CF366C] Dima ans Salad

原题传送门

思路浅析:

这题是01背包的一个变形,只要把a[i]作为价值,a[ i ] - k * b[ i ]作为代价,打一个01背包即可

/* CF366C Dima ans Salad */
#include <bits/stdc++.h>

const int N = 1e5;
int dp[110][N * 2 + 10];
int a[110], b[110], v[110];

inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
        f = (ch == '-') ? -1 : 1, ch = getchar();
    while (isdigit(ch))
        x = x * 10 + (ch - '0'), ch = getchar();
    return x * f;
}

int main()
{
    int n, k;
    n = read(), k = read();
    for (int i = 1; i <= n; i++)
        a[i] = read();
    for (int i = 1; i <= n; i++)
        b[i] = read(), v[i] = a[i] - k * b[i];

    for (int i = 0; i <= N * 2 + 10; i++)
        dp[0][i] = -(1 << 30);
    dp[0][N] = 0;

    for (int i = 1; i <= n; i++)
        for (int j = 2 * N; j >= 0; j--)
            dp[i][j] = std::max(dp[i - 1][j], dp[i - 1][j - v[i]] + a[i]);
    if (dp[n][N] == 0)
        printf("-1\n");
    else
        printf("%d\n", dp[n][N]);
    return 0;
}

转载于:https://www.cnblogs.com/wyctstf/p/11372522.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值