zoukankan      html  css  js  c++  java
  • TOJ-2811 Bessie's Weight Problem(DP、背包问题)

    链接:https://ac.nowcoder.com/acm/contest/1082/K

    题目描述

    Bessie, like so many of her sisters, has put on a few too many
    pounds enjoying the delectable grass from Farmer John's pastures.
    FJ has put her on a strict diet of no more than H (5 <= H <= 45,000)
    kilograms of hay per day.

    Bessie can eat only complete bales of hay; once she starts she can't stop. She has a complete list of the N (1 <= N <= 500) haybales available to her for this evening's dinner and, of course, wants to maximize the total hay she consumes. She can eat each supplied bale only once, naturally (though duplicate weight valuess might appear in the input list; each of them can be eaten one time).
    Given the list of haybale weights Wi (1 <= Wi <= H), determine the maximum amount of hay Bessie can consume without exceeding her limit of H kilograms (remember: once she starts on a haybale, she eats it all).
    POINTS: 250
     

    贝茜像她的诸多姐妹一样,因为从约翰的草地吃了太多美味的草而长出了太多的赘肉.所以约翰将她置于一个及其严格的节食计划之中.她每天不能吃多过H(5≤日≤45000)公斤的干草.贝茜只能吃一整捆干草;当她开始吃一捆干草的之后就再也停不下来了.她有一个完整的N(1≤N≤500)捆可以给她当作晚餐的干草的清单.她自然想要尽量吃到更多的干草.很自然地,每捆干草只能被吃一次(即使在列表中相同的重量可能出现2次,但是这表示的是两捆干草,其中每捆干草最多只能被吃掉一次).

    给定一个列表表示每捆干草的重量Si(1≤Si≤H),求贝茜不超过节食的限制的前提下可以吃掉多少干草(注意一旦她开始吃一捆干草就会把那一捆干草全部吃完).

    输入描述:

    * Line 1: Two space-separated integers: H and N
    * Lines 2..N+1: Line i+1 describes the weight of haybale i with a single integer: Wi

    输出描述:

    * Line 1: A single integer that is the number of kilograms of hay that Bessie can consume without going over her limit.

    示例1

    输入

    56 4
    15
    19
    20
    21

    输出

    56

    说明

    Four haybales of weight 15, 19, 20, and 21. Bessie can eat as many as she wishes without exceeding the limit of 56 altogether.
    Bessie can eat three bales (15, 20, and 21) and run right up to the limit of 56 kg.

    一维01背包模板就能过,价值就相当于重量,是一样的,所以用一位数组就可以存了,下标代表重量。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 #include <set>
    10 #include <stack>
    11 #include <map>
    12 #include <math.h>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 const int maxn=1e5+10;
    17 using namespace std;
    18 
    19 int h,n;
    20 int A[505];
    21 int DP[50005]; 
    22 
    23 int main()
    24 {
    25     scanf("%d %d",&h,&n);
    26     for(int i=1;i<=n;i++)
    27     {
    28         scanf("%d",&A[i]);
    29     }
    30     for(int i=1;i<=n;i++)
    31     {
    32         for(int j=h;j>=A[i];j--)
    33         {
    34             DP[j]=max(DP[j],DP[j-A[i]]+A[i]);
    35         }
    36     }
    37     printf("%d
    ",DP[h]);
    38     return 0;
    39 }

    注:

    -

  • 相关阅读:
    【详记MySql问题大全集】四、设置MySql大小写敏感(踩坑血泪史)
    【详记MySql问题大全集】三、安装之后没有my.ini配置文件怎么办
    【详记MySql问题大全集】二、安装并破解Navicat
    【详记MySql问题大全集】一、安装MySql
    【从零开始搭建自己的.NET Core Api框架】(五)由浅入深详解CORS跨域机制并快速实现
    Redhat6.5编译安装MySQL5.6.38详解
    聚宽常用函数汇总
    小象机器学习(邹博老师)学习笔记
    Python画图色板
    kaggle注册、短信验证终极解决方案(亲测181205)
  • 原文地址:https://www.cnblogs.com/jiamian/p/11433742.html
Copyright © 2011-2022 走看看