TOJ 2838.The Eating Puzzle
2016-09-16 01:05
155 查看
题目链接:http://acm.tju.edu.cn/toj/showp2838.html
2838. The Eating Puzzle
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 1489 Accepted Runs: 776 Multiple test files
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.
Source: USACO
2006 December Competition
Submit List
Runs Forum Statistics
01背包,过之:
#include <stdio.h>
#include <string.h>
#include <algorithm>
int dp[35001];
int a[22];
int main(){
int num,n;
while(~scanf("%d%d",&num,&n)){
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
for(int j=num;j>=a[i];j--)
dp[j]=std::max(dp[j],dp[j-a[i]]+a[i]);
printf("%d\n",dp[num]);
}
}
2838. The Eating Puzzle
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 1489 Accepted Runs: 776 Multiple test files
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.
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
Source: USACO
2006 December Competition
Submit List
Runs Forum Statistics
01背包,过之:
#include <stdio.h>
#include <string.h>
#include <algorithm>
int dp[35001];
int a[22];
int main(){
int num,n;
while(~scanf("%d%d",&num,&n)){
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
for(int j=num;j>=a[i];j--)
dp[j]=std::max(dp[j],dp[j-a[i]]+a[i]);
printf("%d\n",dp[num]);
}
}
相关文章推荐
- TOJ2838 The Eating Puzzle (01背包)
- TOJ 4349: The Tallest in the Class
- TOJ 4008 The Leaf Eaters(容斥定理)
- ZOJ3541-The Last Puzzle 区间dp
- Zoj 3541 The Last Puzzle(区间dp)
- 2015‘12杭电校赛1003 The collector’s puzzle(双指针水题)
- TOJ 2968. Find the Diagonal
- Java Puzzlers笔记--puzzle 5: The joy of Hex 十六进制计算问题
- On Hat Puzzle 2.1: From Dr. Todd Ebert's thesis
- TOJ 3984 I guess the gift is an ipad!
- TOJ 4078 find the princessI(模拟)
- TOJ 3990.I guess the gift is a bag! II(质因数、欧拉函数好题)
- TOJ 1662 The Fewest Coins -- 动态规划DP
- PKU3239 Solution to the n Queens Puzzle
- TOJ-2234. A+B In the Future
- TOJ 2429 Find the Clones
- ZOJ 3541 The Last Puzzle(经典区间dp)
- TOJ 4079 find the princessII(简单博弈)
- TOJ 1188.Tian Ji -- The Horse Racing
- zoj 3541 The Last Puzzle