zoukankan      html  css  js  c++  java
  • POJ 2063 完全背包

    Sample Input

    1
    10000 4
    2
    4000 400
    3000 250

    Sample Output

    14050

    题意: 给你本金 m 和年限 n ,以及 d 种债券(购买一年后就可以卖出)的花费及收益(卖出后的净利润) 
      在 d 种债券中不限制地购买(如果钱够) 问 n 年后的最大收益(含本金)

    m <= 1000000
    n <= 40
    d <= 10


    题目中给出了两个关键的信息 : 债券的花费是1000的倍数,利率不超过10%

    因为花费是1000的倍数,所以可以将dp的复杂度降低1000倍,数组空间同样会压缩1000倍,避免了TIE。
    1.1^40 ≈ 45.3 所以开 45300 的数组就够了。


    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 5e4+5;
    int w[15],v[15];
    int dp[maxn];
    
    int main(){
        int t, m, n, d;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d%d",&m,&n,&d);
            int sum = m;
            m /= 1000;
            for(int i=0;i<d;i++)
                scanf("%d%d",&v[i],&w[i]),v[i]/=1000;
            for(int k=0;k<n;k++){
                memset(dp,0,sizeof(dp[0])*(m+5));
                for(int i=0;i<d;i++){
                    for(int j=v[i];j<=m;j++)
                        dp[j] = max(dp[j],dp[j-v[i]]+w[i]);
                }
                sum += dp[m];
                m = sum/1000;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }#include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 5e4+5;
    int w[15],v[15];
    int dp[maxn];
    
    int main(){
        int t, m, n, d;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d%d",&m,&n,&d);
            int sum = m;
            m /= 1000;
            for(int i=0;i<d;i++)
                scanf("%d%d",&v[i],&w[i]),v[i]/=1000;
            for(int k=0;k<n;k++){
                memset(dp,0,sizeof(dp[0])*(m+5));
                for(int i=0;i<d;i++){
                    for(int j=v[i];j<=m;j++)
                        dp[j] = max(dp[j],dp[j-v[i]]+w[i]);
                }
                sum += dp[m];
                m = sum/1000;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
    View Code
     
     
  • 相关阅读:
    memory addresses
    ddt ddl dml
    PHP Architecture
    disk_free_space
    SAPI
    Simple Mail Transfer Protocol
    AllowOverride None
    function &w(){}
    The History of Operating Systems
    are not called implicitly
  • 原文地址:https://www.cnblogs.com/kongbb/p/10938184.html
Copyright © 2011-2022 走看看