D - Cut Ribbon (dp)

本文介绍了一道关于如何将固定长度的绸缎按照指定长度进行切割的问题,目标是在遵循给定长度约束的情况下,尽可能多地切割出绸缎段落。文章详细解析了解决方案的思路,并提供了一个使用动态规划算法实现的具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                          D -Cut Ribbon

                  Time Limit:2000MS    Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    

Description

Polycarpus has a ribbon, its length isn. He wants to cut the ribbon in a way that fulfils the following two conditions:

  • After the cutting each ribbon piece should have lengtha, b orc.
  • 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 integersn, 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

Input
5 5 3 2
Output
2
Input
7 5 5 2
Output
2



题目解析:

    题目说给出给出一个绸缎的长度n。然后,是三个数,a,b,c。就是表示绸缎可以剪成a,b,c的长度。求,最多可以剪多少段?

思路解析:

    刚看到题的时候,把or看成and了,靠。一开始用母函数写了一个。wrong了。之后才知道是一道dp。我们可以把n看成背包容积,把a,b,c看成不同的花费。则状态转移方程式为dp[i] = max(dp[i],dp[i-x]+1)(i-x>=0)表示当长度为i的时候考虑放x的性价比是多少。所以,我们可以用一个for就可以解决该问题了。


#include <stdio.h>
#include <string.h>

const int INF = ~0U >>2;
const int N = 4e3 + 5;
int n,dp[N];

void Init()
{
    for(int i = 0;i <= n;++i)
      dp[i] = -INF;
}
int main()
{
    int a,b,c;
    while(~scanf("%d%d%d%d",&n,&a,&b,&c))
    {
        Init();dp[0] = 0;//
        for(int i = 1;i <= n;++i)
        {
            if(i - a>=0){
               if(dp[i] < dp[i-a]+1)
                  dp[i] = dp[i-a]+1;
            }
            if(i - b >=0){
               if(dp[i] < dp[i-b]+1)
                 dp[i] = dp[i-b]+1;
            }
            if(i - c >=0){
               if(dp[i] < dp[i-c]+1)
                 dp[i] = dp[i-c]+1;
            }
        }
        printf("%d\n",dp[n]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值