题解 | The Eating-牛客假日团队赛5E题

E-The Eating Puzzle_牛客假日团队赛5

https://ac.nowcoder.com/acm/contest/984/E

题目描述

Bessie is on a diet where she can eat no more than calories per day. Farmer John is teasing her by putting out buckets of feed, each with some (potentially non-unique) number of calories (range: 1..35,000). Bessie has no self-control: once she starts on a feed bucket, she consumes all of it.
Bessie is not so good at combinatorics. Determine the optimal combination of feed buckets that gives Bessie as many calories without exceeding the limit C.
As an example, consider a limit of 40 calories and 6 buckets with 7, 13, 17, 19, 29, and 31 calories. Bessie could eat 7 + 31 = 38 calories but could eat even more by consuming three buckets: 7 + 13 + 19 = 39 calories. She can find no better combination.

输入描述:

Line 1: Two space-separated integers: C and B
Line 2: B space-separated integers that respectively name the number of calories in bucket 1, 2, etc.

输出描述:

Line 1: A single line with a single integer that is largest number of calories Bessie can consume and still stay on her diet.

示例1

输入
40 6
7 13 17 19 29 31
输出
39

解答

注意dp数组的初始化,状态方程:

for i=1..N

    forv=V..0

       f[v]=max{f[v],f[v-c[i]]+w[i]};

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int main(){
    int V,n;
    int w[25],dp[35005];
    scanf("%d%d",&V,&n);
    for (int i = 0;i < n;i++) scanf("%d",&w[i]);
    memset(dp,0,sizeof(dp));
    for (int i = 0;i < n;i++){
        for (int j = V;j >= w[i];j--){
            dp[j] = max(dp[j],dp[j-w[i]]+w[i]);
        }
    }
    printf("%d\n",dp[V]);
    return 0;
}

来源:Mizersy
全部评论

相关推荐

比亚迪汽车新技术研究院 硬件工程师 总包21左右 硕士
点赞 评论 收藏
分享
10-09 00:50
已编辑
长江大学 算法工程师
不期而遇的夏天:1.同学你面试评价不错,概率很大,请耐心等待;2.你的排名比较靠前,不要担心,耐心等待;3.问题不大,正在审批,不要着急签其他公司,等等我们!4.预计9月中下旬,安心过节;5.下周会有结果,请耐心等待下;6.可能国庆节前后,一有结果我马上通知你;7.预计10月中旬,再坚持一下;8.正在走流程,就这两天了;9.同学,结果我也不知道,你如果查到了也告诉我一声;10.同学你出线不明朗,建议签其他公司保底!11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
Java抽象带篮子:难蚌,点进图片上面就是我的大头😆
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-21 19:05
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务