HDU 2546
来源:互联网 发布:艺术签名免费设计软件 编辑:程序博客网 时间:2024/06/03 21:32
简单背包,
题目可以这样解释,除去最贵的物品, 然后花掉尽量多的钱,最后再买掉 最贵的那个
这个题的意思就是必须买最贵的那个。
分析:假设我们这里有一种最优解没有买最贵的那个,这样在买最后一个之前,我们的钱是>=5的,还是买最贵的那个,解会更优。所以买最贵的那个只会让解更优。
剩下的就是在买最贵的之前怎么把钱花的最少,当然要剩下5块钱以上。
AC代码:
#include<iostream>#include<vector>#include<string>#include<cstring>#include<algorithm>#include<set>#include<map>#include<stack>#include<cmath>#include<cstdio>#include<cstdlib>using namespace std;const int maxn=1024;int a[maxn];int n, m;int dp[maxn];int main(){ while(scanf("%d", &n), n){ for(int i =1; i<=n;++i) scanf("%d", &a[i]); scanf("%d", &m); if(m<5) {cout<<m<<endl; continue;} sort(a+1 ,a+n+1); memset(dp, 0 ,sizeof(dp)); for(int i =1; i<n;++i) for(int j =m-5; j>=a[i]; --j){ dp[j]=max(dp[j], dp[j-a[i]]+a[i]); } cout<<m-dp[m-5]-a[n]<<endl; } return 0;}
0 0
- HDU 2546
- HDU 2546
- hdu 2546
- HDU 2546
- hdu 2546
- (2546)HDU
- HDU 2546
- hdu--2546
- HDU 2546
- hdu 2546
- HDU 2546
- [HDU] 2546
- HDU-2546
- HDU 2546 饭卡
- HDU 2546 饭卡
- HDU 2546 饭卡
- hdu 2546 饭卡
- hdu 2546 饭卡
- codeforces 554C Kyoya and Colored Balls 推公式
- 删数字
- Opentaps(OFBiz)在Eclipse下调试配置
- HDU 1159
- Spring AOP 学习之静态代理
- HDU 2546
- 事件处理
- C++第九节:多态、虚函数、抽象类
- 爬虫抓取 第三章
- poj1141Brackets Sequence【区间dp+路径记录】
- 带头尾和动画的下拉刷新RecyclerView
- hdu 1072 Nightmare
- [HDU 1011] Starship Troopers 树形dp
- 普通方法求素数与筛法求素数比较