hdu 2126 Buy the souvenirs 01背包
来源:互联网 发布:数字油田软件系统 编辑:程序博客网 时间:2024/04/23 14:09
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126
题意:给出n件物品的价格。求用m元钱最多能买几件物品,在此情况下有多少种方案?
用dj记录j元最多买多少件物品,pj记录j元买最多物品的情况下的方案。注意分为 d[j]<d[j-v[i]]+1 以及 d[j]==d[j-v[i]]+1 两种情况来考虑。
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#define N 550using namespace std;int d[N],p[N],v[N];int main(){ int T,n,m; cin>>T; while(T--) { cin>>n>>m; for(int i=0;i<n;i++) scanf("%d",&v[i]); memset(d,0,sizeof(d)); for(int i=0;i<=m;i++) p[i]=1; for(int i=0;i<n;i++) for(int j=m;j>=v[i];j--) { if(d[j]<d[j-v[i]]+1) { d[j]=d[j-v[i]]+1; p[j]=p[j-v[i]]; } else if(d[j]==d[j-v[i]]+1) p[j]+=p[j-v[i]]; } if(!d[m]) printf("Sorry, you can't buy anything.\n"); else printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",p[m],d[m]); }}
0 0
- HDU 2126 - Buy the souvenirs(01背包)
- hdu 2126 Buy the souvenirs 01背包
- Buy the souvenirs - HDU 2126 背包dp
- HDU 2126 Buy the souvenirs (dp 二维01背包)
- HDU 2126 Buy the souvenirs(DP:01背包)
- 01背包变形 之 hdu 2126 Buy the souvenirs
- HDU 2126 Buy the souvenirs 01背包 输出方案数
- HDU 2126 Buy the souvenirs (01背包)
- hdu 2126 Buy the souvenirs 01背包+计数dp
- hdu 2126 Buy the souvenirs(二维0/1背包)
- hdu 2126 Buy the souvenirs
- hdu 2126 Buy the souvenirs
- Buy the souvenirs(hdu(2126)
- HDU 2126 Buy the souvenirs
- HDU 2126 Buy the souvenirs
- HDU 2126 Buy the souvenirs
- hdu 2126 Buy the souvenirs
- HDU - 2126 Buy the souvenirs
- ReactNative学习 第一节
- red5源码分析---4
- python 查找文件夹下面的所有文件,读取文件
- 283. Move Zeroes
- 剑指offer 数组中重复的数字
- hdu 2126 Buy the souvenirs 01背包
- ubuntu下常用软件记录
- javaweb学习总结(二十)——JavaBean总结
- ROS--service和function区别
- 关键字的作用 static,extern,const,volatile,register,inline
- Nexus入门指南(图文)
- javaweb学习总结(二十一)——JavaWeb的两种开发模式
- memcached简介及java使用方法
- 【博客地址】:KMP字符串匹配算法与next数组