题目地址:传送门
Description
Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:
Help Polycarpus and find the number of ribbon pieces after the required cutting.
Input
The first line contains four space-separated integers n, a, b and c (1 ≤ n, a, b, c ≤ 4000) — the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers a, b and c can coincide.
Output
Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists.
Sample Input
5 5 3 2
2
7 5 5 2
2
Hint
In the first example Polycarpus can cut the ribbon in such way: the first piece has length 2, the second piece has length 3.
In the second example Polycarpus can cut the ribbon in such way: the first piece has length 5, the second piece has length 2
题解:我们可以把n看成背包容积,把a,b,c看成不同的花费。则状态转移方程式为dp[i] = max(dp[i],dp[i-x]+1)(i-x>=0)表示当长度为i的时候考虑放x的性价比是多少。所以,我们可以用一个for就可以解决该问题了。
#include
#include
#include
#include
#include
using namespace std;
int main(){
int n,a[3],dp[4010];
scanf("%d%d%d%d",&n,&a[0],&a[1],&a[2]);
memset(dp, -40, sizeof(dp));
dp[0]=0;
for(int i=0;i<3;i++){
for(int j=a[i];j<=n;j++){
dp[j]=max(dp[j],dp[j-a[i]]+1);
}
}
printf("%d\n",dp[n]);
}