博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TOJ2811: Bessie's Weight Problem(完全背包)
阅读量:5240 次
发布时间:2019-06-14

本文共 1935 字,大约阅读时间需要 6 分钟。

(<---可以点的)

描述

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 W_i (1 <= W_i <= 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).

输入

* 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: W_i

输出

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

样例输入

56 4

15
19
20
21

样例输出

56

提示

INPUT DETAILS:

Four haybales of weight 15, 19, 20, and 21. Bessie can eat as many as she wishes without exceeding the limit of 56 altogether.

OUTPUT DETAILS:

Bessie can eat three bales (15, 20, and 21) and run right up to the limit
of 56 kg.

 
题意:给定H和N,下面N行是每个物品的重量,求不超过H,最多可以到达的重量。完全背包的裸题
代码:
#include
using namespace std;int dp[450010];int main(){ int weight[600]; int h,n; scanf("%d %d",&h,&n); for(int i = 1 ; i <= n ; i++)scanf("%d",&weight[i]); memset(dp,0,sizeof(dp)); for(int i = 1 ; i <= n ;i++){ for(int j = weight[i] ; j <= h ; j++){ if(j >= weight[i]){ dp[j] = max(dp[j],dp[j-weight[i]]+weight[i]); } } } printf("%d\n",dp[h]); return 0;}

转载于:https://www.cnblogs.com/Esquecer/p/8677452.html

你可能感兴趣的文章
[转]关于GCD与多线程
查看>>
NHibernate.3.0.Cookbook第二章第4节的翻译
查看>>
android学习笔记43——图形图像处理3——Path
查看>>
SVN服务器从windows迁移至Linux
查看>>
HDU 1372 Knight Moves 广搜
查看>>
【★】自制网络心理需求大排名!
查看>>
Linux中如何解压iso类型文件
查看>>
写给自己的 SOA 和 RPC 理解
查看>>
opengl绘制三维人物luweiqi
查看>>
【数学/扩展欧几里得/Lucas定理】BZOJ 1951 :[Sdoi 2010]古代猪文
查看>>
[时间序列分析][2]--趋势和(季节)因子
查看>>
求最大公约数算法
查看>>
数独是否合法
查看>>
JAVA注释
查看>>
题解 P1967 【货车运输】
查看>>
P1077 摆花
查看>>
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
查看>>
第六十五天 how can I 坚持
查看>>
第二十一天 how can I 坚持
查看>>
第一百四十六天 how can I坚持
查看>>