牛客假日团队赛5.E
链接:https://ac.nowcoder.com/acm/contest/984/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
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 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.
输出描述:
Line 1: A single line with a single integer that is largest number of calories Bessie can consume and still stay on her diet.
示例1
输入
40 6
7 13 17 19 29 31
输出
39
思路:其实就是一个01背包问题,只不过一个物体的容量和价值是一样的,都是其卡路里数。在卡路里有范围的情况下求出最大的卡路里值。
AC代码:
#include<bits/stdc++.h>
#define INF 0x3F3F3F3F
#define endl '\n'
#define pb push_back
#define css(n) cout<<setiosflags(ios::fixed)<<setprecision(n);
#define sd(a) scanf("%d",&a)
#define sld(a) scanf("%lld",&a)
#define m(a,b) memset(a,b,sizeof a)
#define p_queue priority_queue
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int n,m;
int t;
double a,b;
int dp[35005];
int arr[25];
int main()
{
scanf("%d%d",&n,&m);
//n是最大的卡路里数,m是桶数。
memset(dp,0,sizeof(dp));
for(int i=1;i<=m;i++)
{
scanf("%d",&arr[i]);
}
for(int i=1;i<=m;i++)
{
for(int j=n;j>=arr[i];j--)
{
dp[j]=max(dp[j],dp[j-arr[i]]+arr[i]);
}
}
printf("%d\n",dp[n]);
return 0;
}