TOJ2838 The Eating Puzzle (01背包)
来源:互联网 发布:xmind8 for mac破解版 编辑:程序博客网 时间:2024/06/17 17:13
Bessie is on a diet where she can eat no more than C (10 ≤C ≤ 35,000) calories per day. Farmer John is teasing her by putting out B (1 ≤ B ≤ 21) 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 limitC.
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.
Input
* 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.
Output
* Line 1: A single line with a single integer that is largest number of calories Bessie can consume and still stay on her diet.Sample Input
40 67 13 17 19 29 31
Sample Output
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;}
阅读全文
0 0
- TOJ2838 The Eating Puzzle (01背包)
- TOJ 2838.The Eating Puzzle
- HDU5543 Pick The Sticks(01背包)
- POJ 3260 The Fewest Coins(完全背包+多重背包(转化为01背包))
- hdu1561(树形dp+01背包) The more, The Better
- HDU1561:The more, The Better(树形dp & 01背包)
- Zoj 3541 The Last Puzzle(区间dp)
- ZOJ 3541 - The Last Puzzle(区间dp)
- HDU5543Pick The Sticks(01背包)
- Eating the IT Elephant: Moving from Greenfield Development to Brownfield
- zoj 3541 The Last Puzzle
- ZOJ 3541 The Last Puzzle
- HDU 3496 Watch The Movie(二维01背包)
- poj 2642 The Brick Stops Here(01背包)
- hdu 3496 Watch The Movie(二维01背包)
- uva 357 Let Me Count The Ways(01背包)
- hdu 3496 Watch The Movie(01背包)
- HDU 2126 Buy the souvenirs (01背包)
- c语言之邻接矩阵&最短路径&最小生成树
- 修改,全选,反选
- 论文复现报告:Deep Region and Multi-label Learning for Facial Action Unit Detection
- HDU 2563.统计问题
- 图解设计模式
- TOJ2838 The Eating Puzzle (01背包)
- Android SDK Manager国内无法更新的解决方案
- Python 5种连接字符串的方法
- Navicat for mysql 安装破解教程
- iOS开发多线程-NSOperation \ GCD详解
- springwebsocket部署的一次坑
- 数据结构实验之排序一:一趟快排
- 17.字符串判等
- Android工具类总结