HDU 2126 Buy the souvenirs 01背包 输出方案数
来源:互联网 发布:微信淘客发单软件 编辑:程序博客网 时间:2024/04/26 16:24
题意:有N种纪念品,给出对应的价格。现在有N元钱,想买到最多的纪念品,同时输出所有可能的方案数。
思路:因为纪念品可以买,也可以不买。所以是标准的01背包。
在求最大值的过程中,同时求出对应的方案数即可。注意方案数的计算。
代码如下:
#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int a[50];int dp[550][2];int main(void){ //freopen("input.txt","r",stdin); int T,N,M; scanf("%d",&T); while(T--){ scanf("%d%d",&N,&M); for(int i = 0; i < N; ++i) scanf("%d",&a[i]); for(int i = 0; i <= M; ++i) dp[i][1] = 1,dp[i][0] = 0; for(int i = 0; i < N; ++i){ for(int j = M; j >= a[i]; --j){ if(dp[j][0] < dp[j - a[i]][0] + 1){ dp[j][0] = dp[j-a[i]][0] + 1; dp[j][1] = dp[j-a[i]][1]; } else if(dp[j][0] == dp[j - a[i]][0] + 1) dp[j][1] += dp[j-a[i]][1]; }// for(int j = 0; j <= M; ++j)// printf("%d%c",dp[j][1],j == M?'\n':' '); } if(dp[M][0] == 0) puts("Sorry, you can't buy anything."); else printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",dp[M][1],dp[M][0]); } return 0;}
0 0
- HDU 2126 Buy the souvenirs 01背包 输出方案数
- hdu 2126 Buy the souvenirs (01背包输出方案数)
- NYOJ 2126 Buy souvenirs(01背包+输出方案数)
- HDU-2126 Buy the souvenirs (记录最优方案数的01背包问题 入门题)
- HDU 2126 - Buy the souvenirs(01背包)
- hdu 2126 Buy the souvenirs 01背包
- hdu2126---Buy the souvenirs(01背包方案数)
- 【01背包方案数】HDU2126-Buy the souvenirs
- Buy the souvenirs(方案数+01背包)
- hdu 2126 Buy the souvenirs(求方案数的背包)
- hdu 2126 Buy the souvenirs(求方案数的背包)
- hdu 2126 Buy the souvenirs(求方案数)
- Buy the souvenirs - HDU 2126 背包dp
- hdu 2126 Buy the souvenirs(01背包求最大容量方法数)
- 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背包)
- 关于mysql的登录时1045错误的解决windows下
- 结构体的使用
- 常用UI组件(widgets)之文本编辑框(EditText)(二)
- Structs Convention Plugin
- 2011-05-11 15:38 关于dpkg:处理 xxx (--configure)时出错
- HDU 2126 Buy the souvenirs 01背包 输出方案数
- 【C++基础】----运算符重载 (02)
- 关于Clear调用失败----导致残影
- [D-VI] my_second_fpga(1位加法器 Verilog HDL抽象级别 modelsim-altrea功能仿真 综合器)
- leetcode|80|Remove Duplicates from Sorted Array II
- poj 2828 Buy Tickets 万能的线段树大法。
- LeetCode:Pascal's Triangle II
- 【 随笔 】 JavaScript 图形库的流行度调查
- HDU 1231 最大连续子序列