TOJ2838 The Eating Puzzle (01背包)
2017-12-20 19:48
393 查看
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 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 2: B space-separated integers that respectively name the number of calories in bucket 1, 2, etc.
注意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;
}
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 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.
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 6 7 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;
}
相关文章推荐
- TOJ 2838.The Eating Puzzle
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
- HDU-3496-Watch The Movie(二维01背包问题)
- HDU - 3496 Watch The Movie(01背包)
- uva 590 - Always on the run(01背包)
- PKU3239 Solution to the n Queens Puzzle
- hdu 5543 Pick The Sticks(01背包)
- BZOJ_P3101 N皇后/POJ_P3239 Solution to the n Queens Puzzle(N皇后构造)
- Java Puzzlers笔记--puzzle 5: The joy of Hex 十六进制计算问题
- poj 2642 The Brick Stops Here(01背包)
- 计算机学院大学生程序设计竞赛(2015’12)The collector’s puzzle
- 【HDU】4901 The Romantic Hero 01背包
- POJ3239《Solution to the n Queens Puzzle》方法:构造公式法
- hdu2126——Buy the souvenirs——————【01背包记录物品最多时种数】
- hdu 5543 Pick The Sticks(01背包)
- The Ball Puzzle
- HDU 5543 Pick The Sticks (DP 01背包)
- HDU3496:Watch The Movie(二维01背包)
- POJ3239-Solution to the n Queens Puzzle
- ZOJ3541-The Last Puzzle 区间dp