codeforces 189A

A. Cut Ribbon
time limit per test
1 second
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.

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,将饰带分成a,b或c的长度,最多能分几个。与背包问题很类似,可以做一个表格进行分析,以第一个输入为例

 012345
5000001
3000101
2001102

 

只有完全分解饰带的才会记录在数据中,如果a,b和c只能用一次,中间的递归过程为

for (i = 0;i < number;i++)
        for (j = length;j >= 0;j--)
            if (length >= l[i] && val[j] < val[j - l[i]] + 1)
                val[j] = val[j - l[i]] + 1;

如果可以重复使用,中间的递归过程为

for (i = 0;i < 3;i++)
            for (j = a[i];j <= len;j++)
                if (val[j] < val[j - a[i]] + 1)
                    val[j] = val[j - a[i]] + 1;

两个代码唯一区别就是从len开始还是从前面开始,从len开始当前的长度不会影响结果,从前面开始当前的长度会影响结果。

#include<iostream>
using namespace std;
const int INF = -999999;
int main()
{
    int len, a[5], i, j, ans, val[4003];
    while (scanf("%d%d%d%d", &len, &a[0], &a[1], &a[2]) != EOF)
    {
        for (i = 1;i <= len;i++)
            val[i] = INF;
        val[0] = 0;
        for (i = 0;i < 3;i++)
            for (j = a[i];j <= len;j++)
                if (val[j] < val[j - a[i]] + 1)
                    val[j] = val[j - a[i]] + 1;
        ans = val[len];
        cout << ans << endl;
    }
    return 0;
}

过渡版本为:

#include<stdio.h>
#include<string.h>
double ribbon(int number, int *l, int length)
{
    int i, j, val[4003];
    memset(val, 0, sizeof(val));
    for (i = 0;i < number;i++)
        for (j = length;j >= 0;j--)
            if (length >= l[i] && val[j] < val[j - l[i]] + 1)
                val[j] = val[j - l[i]] + 1;
    return val[length];
}
int main()
{
    int n, len[5];
    scanf("%d", &n);
    for (int i = 0;i < 3;i++)
        scanf("%d",&len[i]);
    int ans;
    ans = ribbon(3, len, n);
    printf("%d\n",ans);
    return 0;
}

 

 

转载于:https://www.cnblogs.com/chenruijiang/p/8250535.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值