• 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
     
     
  • 相关阅读:
    中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结
    各种求逆元
    A*(A_star)搜索总结
    线段树总结
    C++的STL
    Unable to make the session state request to the session state server处理方法
    判断UserAgent是否来自微信
    VS2010 EntityFramework Database First
    VS2010类似Eclipse文件查找功能-定位到
    Newtonsoft.Json随手记
  • 原文地址:https://www.cnblogs.com/kongbb/p/10938184.html
Copyright © 2020-2023  润新知