Cut Ribbon (恰好装满的完全背包)
Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:
After the cutting each ribbon piece should have length a, b or c.
After the cutting the number of ribbon pieces should be maximum.
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.
Examples
Input
5 5 3 2
Output
2
Input
7 5 5 2
Output
2
Note
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三种,问最多能有几条
思路: 恰好完全装满的完全背包
AC代码:
//初始化的dp数组事实上就是在没有任何物品可以放入背包时的合法状态。
//如果要求背包恰好装满,那么此时只有容量为0的背包可以在什么也不装的状态下被 “恰好装满” ,此时背包价值为0。
//其他容量的背包均没有合法的解,属于未定义的状态,所以都应该被赋值为 ∞ 。(求最大值赋负无穷,求最小值赋正无穷)
//当前的合法解,一定是从之前的合法状态推得的
//如果背包并非必须被装满,那么任何容量的背包都有一个合法解 “什么也不装”,这个解的价值为0,
//所以初始化时状态的值也就全部为0了。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int d[4100],dp[4100];//装满的完全背包
int main()
{
int n,a,b,c,i,j;
scanf("%d%d%d%d",&n,&a,&b,&c);
d[0]=a,d[1]=b,d[2]=c;
memset(dp,-999999,sizeof(dp));
dp[0]=0;
for(i=0;i<3;i++)
{
for(j=d[i];j<=n;j++)
{
dp[j]=max(dp[j],dp[j-d[i]]+1);
}
}
printf("%d\n",dp[n]);
return 0;
}
该博客探讨了一个关于丝带切割的问题,目标是将其长度剪裁成a、b、c三种,使得剪裁后丝带段数最多。通过介绍一个恰好装满的完全背包问题,博主展示了如何使用动态规划来解决这个问题,以找到最多可能的丝带段数。AC代码展示了具体的实现过程。
167万+

被折叠的 条评论
为什么被折叠?



