zoukankan      html  css  js  c++  java
  • 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 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
    Copy
    5 5 3 2
    output
    Copy
    2
    input
    Copy
    7 5 5 2
    output
    Copy
    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.


    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <unordered_set>
    #include <unordered_map>
    #define ll long long
    #define mod 998244353
    using namespace std;
    int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
    
    
    int main() 
    {
        int n;
        cin >> n;
        vector<int> a(3),dp(n+1);
        for (int i = 0; i < 3; i++)
            cin >> a[i];
        sort(a.begin(), a.end());
        if (a[0] <= n) dp[a[0]] = 1;
        if (a[1] <= n) dp[a[1]] = 1;
        if (a[2] <= n) dp[a[2]] = 1;
        for (int i = 1; i <= n; i++)
        {
            int ans = dp[i];
            if (i > a[0] && dp[i - a[0]] != 0) ans = max(ans, dp[i - a[0]] + 1);
            if (i > a[1] && dp[i - a[1]] != 0) ans = max(ans, dp[i - a[1]] + 1);
            if (i > a[2] && dp[i - a[2]] != 0) ans = max(ans, dp[i - a[2]] + 1);
            dp[i] = ans;
        }
        cout << dp[n] << endl;
        return 0;
    }
  • 相关阅读:
    固定思维的可怕(转)
    Javascript模块化编程:require.js的用法
    js中将字符串转为JSON的三种方式
    cf 55D 数位dp 好题
    fzu 2113 数位dp
    uestc 250 数位dp(水)
    hdu 3652数位dp
    数位dp 3943 二分法
    hdu 3943 经典数位dp好题
    hdu 4871 树的分治+最短路记录路径
  • 原文地址:https://www.cnblogs.com/dealer/p/12325505.html
Copyright © 2011-2022 走看看