Buy the souvenirs

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
When
the winter holiday comes, a lot of people will have a trip. Generally,
there are a lot of souvenirs to sell, and sometimes the travelers will
buy some ones with pleasure. Not only can they give the souvenirs to
their friends and families as gifts, but also can the souvenirs leave
them good recollections. All in all, the prices of souvenirs are not
very dear, and the souvenirs are also very lovable and interesting. But
the money the people have is under the control. They can’t buy a lot,
but only a few. So after they admire all the souvenirs, they decide to
buy some ones, and they have many combinations to select, but there are
no two ones with the same kind in any combination. Now there is a blank
written by the names and prices of the souvenirs, as a top coder all
around the world, you should calculate how many selections you have, and
any selection owns the most kinds of different souvenirs. For instance:

And
you have only 7 RMB, this time you can select any combination with 3
kinds of souvenirs at most, so the selections of 3 kinds of souvenirs
are ABC (6), ABD (7). But if you have 8 RMB, the selections with the
most kinds of souvenirs are ABC (6), ABD (7), ACD (8), and if you have
10 RMB, there is only one selection with the most kinds of souvenirs to
you: ABCD (10).

 
Input
For the first line, there is a T means the number cases, then T cases follow.
In
each case, in the first line there are two integer n and m, n is the
number of the souvenirs and m is the money you have. The second line
contains n integers; each integer describes a kind of souvenir.
All
the numbers and results are in the range of 32-signed integer, and
0<=m<=500, 0<n<=30, t<=500, and the prices are all
positive integers. There is a blank line between two cases.
 
Output
If
you can buy some souvenirs, you should print the result with the same
formation as “You have S selection(s) to buy with K kind(s) of
souvenirs”, where the K means the most kinds of souvenirs you can buy,
and S means the numbers of the combinations you can buy with the K kinds
of souvenirs combination. But sometimes you can buy nothing, so you
must print the result “Sorry, you can't buy anything.”
 
Sample Input
2
4 7
1 2 3 4

4 0
1 2 3 4

 
Sample Output
You have 2 selection(s) to buy with 3 kind(s) of souvenirs.
Sorry, you can't buy anything.
 
Author
wangye
 
Source

思路:在01背包的方案dp方程再加一维,记录K;

    dp[i][t]+=dp[i-a[j]][t-1];

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define esp 0.00000000001
const int N=1e3+,M=1e6+,inf=1e9+,mod=;
int a[N];
int dp[N][N];
int main()
{
int x,y,z,i,t;
int T;
scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof(dp));
scanf("%d%d",&x,&y);
for(i=;i<=x;i++)
scanf("%d",&a[i]);
dp[][]=;
for(i=;i<=x;i++)
{
for(t=y;t>=a[i];t--)
{
for(int j=;j<x;j++)
dp[t][j+]+=dp[t-a[i]][j];
}
}
int ans=;
for(t=x;t>=;t--)
{
ans=;
for(i=;i<=y;i++)
ans+=dp[i][t];
if(ans>)break;
}
if(t>)
printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",ans,t);
else
printf("Sorry, you can't buy anything.\n");
}
return ;
}

hdu 2126 Buy the souvenirs 二维01背包方案总数的更多相关文章

  1. hdu3496 二维01背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M ...

  2. HDU 2126 Buy the souvenirs (01背包,输出方案数)

    题意:给出t组数据 每组数据给出n和m,n代表商品个数,m代表你所拥有的钱,然后给出n个商品的价值 问你所能买到的最大件数,和对应的方案数.思路: 如果将物品的价格看做容量,将它的件数1看做价值的话, ...

  3. hdu 2126 Buy the souvenirs 【输出方案数】【01背包】(经典)

    题目链接:https://vjudge.net/contest/103424#problem/K 转载于:https://blog.csdn.net/acm_davidcn/article/detai ...

  4. hdu 2126 Buy the souvenirs(记录总方案数的01背包)

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. hdu 2126 Buy the souvenirs 买纪念品(01背包,略变形)

    题意: 给出一些纪念品的价格,先算出手上的钱最多能买多少种东西k,然后求手上的钱能买k种东西的方案数.也就是你想要买最多种东西,而最多种又有多少种组合可选择. 思路: 01背包.显然要先算出手上的钱m ...

  6. HDU 2159 FATE (DP 二维费用背包)

    题目链接 题意 : 中文题不详述. 思路 : 二维背包,dp[i][h]表示当前忍耐值为i的情况下,杀了h个怪得到的最大经验值,状态转移方程: dp[i][h] = max(dp[i][h],dp[i ...

  7. Leetcode_474. 一和零(二维01背包)

    每个字符串看成一个物品,两个属性是0和1的个数,转换为01背包. code class Solution { public: int w[605][2]; int dp[105][105]; int ...

  8. HDU--2126 Buy the souvenirs(二维01背包)

    题目http://acm.hdu.edu.cn/showproblem.php?pid=2126 分析:有两个要求,一是计算最多可以选多少中纪念品:而是计算选最多纪念品的方案有多少种, 即统计最优方案 ...

  9. [HDU 2126] Buy the souvenirs (动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意:给你n个物品,m元钱,问你最多能买个多少物品,并且有多少种解决方案. 一开始想到的是,先解 ...

随机推荐

  1. 搭建SVN和eclipse添加svn插件

    在ubuntu服务器下安装svn服务器端,在window下使用eclipse的svn插件团队开发. 安装SVN服务端 1.在ubuntu上安装svn服务器 sudo apt-get install s ...

  2. shell输出颜色

    #!/bin/bash # #下面是字体输出颜色及终端格式控制 #字体色范围:- echo -e "\033[30m 黑色字 \033[0m" echo -e "\033 ...

  3. The OpenCV Coding Style Guide

    https://github.com/opencv/opencv/wiki/Coding_Style_Guide

  4. 聊天系统 websocket 直播聊天

    websocket http://kai.yilaizhibo.com/js/controller.js http://kai.yilaizhibo.com/js/controller.js $(fu ...

  5. ES6通过WeakMap解决内存泄漏问题

    一.Map 1.定义 Map对象保存键值对,类似于数据结构字典:与传统上的对象只能用字符串当键不同,Map对象可以使用任意值当键. 2.语法 new Map([iterable]) 属性 size:返 ...

  6. Struts2 框架的值栈

    1. OGNL 表达式 1.1 概述 OGNL(Object Graphic Navigation Language),即对象图导航语言; 所谓对象图,即以任意一个对象为根,通过OGNL可以访问与这个 ...

  7. CSS 快速入门

    特点: CSS 将网页内容和显示样式进行分离,提高了显示效果的功能. CSS 和 html 相结合的四种方式: style 属性的方式 每个 html 标签中都有一个 style 样式属性, 该属性的 ...

  8. 【人员招聘】岗位职责和技能要求——By Me

  9. golang SQLite3性能测试

    SQLite是个小型的数据库,很简洁,即支持文件也支持内存,比较适合小型的独立项目,在没有网络的时候做一些复杂的关系数据存储和运算. 为了考察性能做10M(1000万)条记录的测试,测试机4CPU.8 ...

  10. 35个例子学会find

    find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> - <指定目录>: 所要搜索的目录及其所有子目录.默认为当前目录. - ...