Cut Ribbon


Cut Ribbon
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ab 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 nab 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 ab 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 test(s)
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.

一个绳子,可以分成a,b,c长度,最多分成多少段?

一开始的暴力方法,全分成a,b,c的段数,然后暴力判断,然后超时- -。接着就是剪枝,贪心的想法,并且缩小b,c段数,即确定a之后再求b,c的段数

如果有,最大段数就是这个。然而有反例,比如53,10,11,23.那么在3个10,一个23就会退出,实际上2个10,3个11才是最优解。因此在判断b,c段的时候不可以跳出,而是用max来找。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int max(int a, int b)
{
 return a > b ? a : b;
}
int main()
{
 int i, j, m, n, a, b, c, ans, pre1, pre2, pre3, k, temp, leap = 0, temp1;
 ans = 0;
 cin >> n >> a >> b >> c;
 if (a > b){ temp = a; a = b; b = temp; }
 if (b > c){ temp = b; b = c; c = temp; }
 if (a > b){ temp = a; a = b; b = temp; }
 pre1 = n / a;
 if (n%a == 0)
  ans = n / a;
 else
 {
  for (i = pre1; i >= 0; i--)
  {
   temp = n - a*i;
   if (temp%b == 0)
   {
    ans = max(ans, temp / b + i);
   }
   else{
    pre2 = temp / b;
    for (j = pre2; j >= 0; j--)
    {
     temp1 = temp - b*j;
     if (temp1%c == 0)
     {
      ans = max(ans, i + j + temp1 / c);
      break;
     }
    }
   }
  }
 }
 cout << ans << endl;
 return 0;
}

2.网上用的是dp法,看的我眼都要瞎了。离散化dp,然后dp[i+a]=max(dp[i+a],dp[i]+1);

然后最后输出dp[n],还需多练。还有就是从前向后找需要注意数组大小的问题,或者加一个判断if(i+a<=n)


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int dp[100000];
int max(int a, int b)
{
 return a>b ? a : b;
}
int main()
{
 int a, b, c, n, i, j;
 cin >> n >> a >> b >> c;
 memset(dp, 0, sizeof(dp));
 dp[a] = dp[b] = dp[c] = 1;
 for (i = 1; i <= n; i++)
 {
  if (dp[i])
  {
   dp[i + a] = max(dp[i + a], dp[i] + 1);
   dp[i + b] = max(dp[i + b], dp[i] + 1);
   dp[i + c] = max(dp[i + c], dp[i] + 1);
  }
 }
 cout << dp[n] << endl;
 return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值