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.
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.
Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists.
5 5 3 2
2
7 5 5 2
2
小结:果然,dp我不是一般的弱,真的真的还需要多多锻炼.....
以下是AC代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define mnum (~0U>>2)
int n,dp[4100];
void Init()
{
//printf("%d\n\n",-mnum);
for(int i=0;i<=n;i++)
dp[i]=-mnum;
}
int main()
{
int a,b,c;
while(~scanf("%d%d%d%d",&n,&a,&b,&c))
{
Init();
//printf("%d\n",dp[0]);
dp[0]=0;
for(int i=1;i<=n;i++)
{
if(i-a>=0)
dp[i]=dp[i-a]+1>dp[i]?dp[i-a]+1:dp[i];
if(i-b>=0)
dp[i]=dp[i-b]+1>dp[i]?dp[i-b]+1:dp[i];
if(i-c>=0)
dp[i]=dp[i-c]+1>dp[i]?dp[i-c]+1:dp[i];
}
printf("%d\n",dp[n]);
}
return 0;
}
本文探讨了如何使用动态规划解决切割特定长度丝带的问题,目标是在切割后得到尽可能多的长度为a、b或c的丝带片段。通过输入丝带原始长度n及可接受的片段长度a、b、c,算法输出最优的片段数量。实例展示了解决过程,并提供了AC代码作为参考。
549

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



