HDU - 2546 饭卡
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546
#include <iostream> #include <string.h> #include <algorithm> using namespace std; /**************************************************************************************************************** 题意: 饭卡余额 >= 5时可以饥饿消费,求饭卡最小余额 思路: 1,今天唯一一个有点技术的dp了,之前傻傻的排序求,太蠢了 2,技巧,先花5元买最贵的蔬菜(有点贪心的思想) 3,然后就是 V-5 的 0/1背包问题了,最大花费是 V-5 ****************************************************************************************************************/ int v[1050]; int dp[1050]; int main() { int n,m; while(cin>>n) { if(n == 0) break; memset(v,0,sizeof(v)); for(int i = 1;i <= n;i ++) cin>>v[i]; cin>>m; sort(v+1,v+n+1); if(m < 5) cout<<m<<endl; else{ memset(dp,0,sizeof(dp)); for(int i = 1;i < n;i ++){ for(int j = m-5;j >= v[i];j --) dp[j]=max(dp[j],dp[j-v[i]]+v[i]); } cout<<m-v[n]-dp[m-5]<<endl; } } return 0; }