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

HDU2546:饭卡(01背包)

时间:2014-10-17 01:36:13      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   strong   

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

当我们遇到问题选择物体的价值和顺序相关时就需要,排完序后对其01处理。这题因为当我们小的先点的话则越接近5,然后我们一次取最大值,则我们花的钱就越多。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
#define N 1005
int dp[N+50];
int main(void)
{
    int num[N];
    int n,m;
    int i,j;
    while(cin>>n&&n!=0){
        for(i=1;i<=n;i++) cin>>num[i];
        cin>>m;
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        int ma=0;
        sort(num+1,num+1+n);
        for(i=1;i<=n;i++){
            for(j=m+45;j>=0;j--){
                if((m-j)>=5&&dp[j]){ 
                    dp[j+num[i]]=1;
                    ma=max(ma,j+num[i]);
                }
            }
        }
        
        cout<<m-ma<<endl;
    }
    return 0;
}

 

HDU2546:饭卡(01背包)

标签:style   blog   http   color   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/woshijishu3/p/4029841.html

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