Cut Ribbon(恰好装满的完全背包)

该博客探讨了一个关于丝带切割的问题,目标是将其长度剪裁成a、b、c三种,使得剪裁后丝带段数最多。通过介绍一个恰好装满的完全背包问题,博主展示了如何使用动态规划来解决这个问题,以找到最多可能的丝带段数。AC代码展示了具体的实现过程。

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;
 } 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值