码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2546 01背包问题

时间:2016-04-04 17:50:15      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

#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\n",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\n",money+5-dp[money]-price[n-1]);
    }
    return 0;
} 

 

HDU 2546 01背包问题

标签:

原文地址:http://www.cnblogs.com/sasuke-/p/5352368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!