zoukankan      html  css  js  c++  java
  • HDU 2546 01背包问题

    这里5元是个什么意思呢、差不多就是特殊情况了、

    就是说最贵的那个东西先不买、并且最后要留下5元去买那个最贵的、

    也就是说对现在金钱-5 拿剩下的钱去对减去最贵的商品后的商品dp、看这些剩下的钱能买多少价值的商品

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    const int qq=1000+10;
    int price[qq<<1],dp[qq<<1];
    int main()
    {
        int n;
        while(~scanf("%d",&n)&&n){
            memset(price,0,sizeof(price));    //初始化 
            memset(dp,0,sizeof(price));
            for(int i=0;i<n;++i)
                scanf("%d",&price[i]);
            sort(price,price+n);
            int money;scanf("%d",&money);
            if(money<5){
                printf("%d
    ",money);
                continue;
            }
            money=money-5;        //取出5元用于购买最贵的物品、
            for(int j,i=0;i<n-1;++i)
                for(j=money;j>=price[i];--j)
                    dp[j]=max(dp[j],dp[j-price[i]]+price[i]);    //物品只有买或者不买两种选择
            printf("%d
    ",money+5-dp[money]-price[n-1]);
        }
        return 0;
    } 
  • 相关阅读:
    python3之datetime模块
    python3之time模块
    前端面试题01
    前端面试题02
    angularjs
    nodejs
    android 报错记录
    android知识点回顾二
    android知识点回顾
    Broadcast广播代码例子
  • 原文地址:https://www.cnblogs.com/sasuke-/p/5352368.html
Copyright © 2011-2022 走看看