zoukankan      html  css  js  c++  java
  • dp第四题

    #include <string.h>
    #include <stdio.h>
    #include <iostream>
    using namespace std;
    int dp[4050];
    int main()
    {
        int n,a,b,c;
        while(cin >> n >> a >> b >> c)
        {
            memset(dp,-1,sizeof(dp));
            dp[a]=1;dp[b]=1;dp[c]=1;
            for(int i=1;i<=n;i++)
            {
                if(i-a>0 && dp[i-a]!=-1 && dp[i-a]+1>dp[i]) dp[i]=dp[i-a]+1;
                if(i-b>0 && dp[i-b]!=-1 && dp[i-b]+1>dp[i]) dp[i]=dp[i-b]+1;
                if(i-c>0 && dp[i-c]!=-1 && dp[i-c]+1>dp[i]) dp[i]=dp[i-c]+1;
            }
    //        for(int i=1;i<=n;i++)
    //        {
    //            cout << dp[i] << " ";
    //        }
            cout << dp[n] << endl;
        }
        return 0;
    }

    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.

    Input

    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.

    Output

    Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists.

    某晚codeforces上的2房A题。

    状态:dp[i]代表结到长度为i时,所能结的最大段数。

    状态转移方程:dp[i]=dp[i-a]+1;

    考虑三个情况:

    1.减出界;

    2.初始值为-1,不进行处理,因为无法形成一段的剪法,再加一块,也无法形成正确的剪法;

    3.dp[i]<dp[i-a]+1,求最大的段数,所以dp[i]要比dp[i-1]存的段数多。

  • 相关阅读:
    Django-admin导出到表格
    Django1.11.4中文文档
    Django+uwsgi+nginx+angular.js项目部署
    angular.js 入门
    枚举类型的总结
    基于websocket的页面聊天程序
    java网络编程(三):一个类似QQ的聊天程序
    java网络编程(二)
    java网络编程(一)
    java中的序列化与反序列化,还包括将多个对象序列化到一个文件中
  • 原文地址:https://www.cnblogs.com/markliu/p/2499202.html
Copyright © 2011-2022 走看看