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;
    }
  • 相关阅读:
    Python字符编码详解
    Python 编程规范
    希尔排序
    浅析 Python 的 metaclass
    c#通过数据集生成浏览页面
    QQ搜索群参数详解
    扩展名为HTM或HTML的文件图标不能正常显示的解决方案
    利用QQ2009协议,将抓包直接解密出ClientKey(SessionKey)
    用Sql语句还原,分离,删除数据库连接
    asp.net 导出excel 问题 (服务器的部署)
  • 原文地址:https://www.cnblogs.com/dealer/p/12325505.html
Copyright © 2011-2022 走看看