zoukankan      html  css  js  c++  java
  • HDU 2546(01背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=2546

    http://blog.csdn.net/xujinsmile/article/details/7969412

    首先拿出5元买最贵的东西,那接下来就是背包容量m-5,物品数量n-1 的01背包问题了。

    做背包问题一定要明确边界条件再入手,不然很费时间

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    using namespace std;
    
    #define MEM(a,b) memset(a,b,sizeof(a))
    #define pf printf
    #define sf scanf
    #define debug printf("!
    ")
    #define INF 8000
    #define MAX(a,b) a>b?a:b
    #define blank pf("
    ")
    #define LL long long
    #define ep 1e-6
    
    int dp[INF];
    
    int ci[INF];//容量
    int wi[INF];//价值
    int n,V,i,j,v,t,sum;
    
    
    void zeroOnePack(int cost,int weight)
    {
              for(v = V-5;v>=cost;v--)
              {
                        dp[v] =MAX(dp[v],dp[v-cost]+weight);
              }
    }
    
    void completePack(int cost,int weight)
    {
              for(v = cost;v<=V;v++)
              {
                        dp[v] =MAX(dp[v],dp[v-cost]+weight);
                        pf("tt%d %d %d
    ",i,v,dp[v]);
              }
    
    }
    
    int main()
    {
    
              int t;
              while(sf("%d",&n) && n)
              {
    
                        MEM(dp,0);
                        MEM(ci,0);
                        MEM(wi,0);
    
                        for(i = 1;i<=n;i++)
                        {
                                  sf("%d",&wi[i]);
                        }
    
                        sort(wi+1,wi+n+1);
    
                        sf("%d",&V);
    
                        int m = wi[n];
    
                        if(V<5)
                        {
                                  pf("%d
    ",V);
                                  continue;
                        }
                        else
                        {
                                  n--;
                                  for(i = 1;i<=n;i++)
                                  {
                                            zeroOnePack(wi[i],wi[i]);
                                  }
    
                                  pf("%d
    ",V-m-dp[V-5]);
    
    
                        }
              }
        return 0;
    }
  • 相关阅读:
    钉钉 LDAP
    OpenLDAP 密码策略与审计控制
    Active Directory LDAP DingDing
    Linux kill 命令 java
    Memory Analyzer 与 Java VM 版本支持问题
    java.lang.Thread.State
    稻盛和夫 活法 人生公式
    [领导力/管理]一句话说带团队
    把某个公司git项目迁移到gitee的步骤
    Protocol Buffers  |  Google Developers
  • 原文地址:https://www.cnblogs.com/qlky/p/5034519.html
Copyright © 2011-2022 走看看