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;
    }
  • 相关阅读:
    Linux常用命令2
    Linux常用命令1
    Nginx配置Kafka
    SpringBoot整合Druid
    spring boot jpa
    mybatis-plus_2
    copy data to map
    HashMap容量问题
    在SpringBoot主启动类中获取实例化的Bean
    Linux环境中Rsync增量备份文件
  • 原文地址:https://www.cnblogs.com/qlky/p/5034519.html
Copyright © 2011-2022 走看看