hdu 2126 Buy the souvenirs 二维01背包方案总数
Buy the souvenirs
Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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).
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.
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.”
4 7
1 2 3 4
4 0
1 2 3 4
Sorry, you can't buy anything.
思路:在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背包方案总数的更多相关文章
- hdu3496 二维01背包
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M ...
- HDU 2126 Buy the souvenirs (01背包,输出方案数)
题意:给出t组数据 每组数据给出n和m,n代表商品个数,m代表你所拥有的钱,然后给出n个商品的价值 问你所能买到的最大件数,和对应的方案数.思路: 如果将物品的价格看做容量,将它的件数1看做价值的话, ...
- hdu 2126 Buy the souvenirs 【输出方案数】【01背包】(经典)
题目链接:https://vjudge.net/contest/103424#problem/K 转载于:https://blog.csdn.net/acm_davidcn/article/detai ...
- hdu 2126 Buy the souvenirs(记录总方案数的01背包)
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 2126 Buy the souvenirs 买纪念品(01背包,略变形)
题意: 给出一些纪念品的价格,先算出手上的钱最多能买多少种东西k,然后求手上的钱能买k种东西的方案数.也就是你想要买最多种东西,而最多种又有多少种组合可选择. 思路: 01背包.显然要先算出手上的钱m ...
- HDU 2159 FATE (DP 二维费用背包)
题目链接 题意 : 中文题不详述. 思路 : 二维背包,dp[i][h]表示当前忍耐值为i的情况下,杀了h个怪得到的最大经验值,状态转移方程: dp[i][h] = max(dp[i][h],dp[i ...
- Leetcode_474. 一和零(二维01背包)
每个字符串看成一个物品,两个属性是0和1的个数,转换为01背包. code class Solution { public: int w[605][2]; int dp[105][105]; int ...
- HDU--2126 Buy the souvenirs(二维01背包)
题目http://acm.hdu.edu.cn/showproblem.php?pid=2126 分析:有两个要求,一是计算最多可以选多少中纪念品:而是计算选最多纪念品的方案有多少种, 即统计最优方案 ...
- [HDU 2126] Buy the souvenirs (动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意:给你n个物品,m元钱,问你最多能买个多少物品,并且有多少种解决方案. 一开始想到的是,先解 ...
随机推荐
- python相关的报错处理
1.python3.6编译安装完毕后,使用pip3安装virtualenv,提示找不到ssl模块 原因:因为我们少装了openssl-devel依赖包,所以导致编译后的pip3无法找到ssl模块. 解 ...
- VC的CListCtrl控件
1. CListCtrl 样式及设置 2. 扩展样式设置 3. 数据插入 4. 一直选中Item 5. 选中和取消选中Item 6. 得到CListCtrl中所有行的checkbox的状态 7. 得到 ...
- Apache Tez Design
http://tez.incubator.apache.org/ http://dongxicheng.org/mapreduce-nextgen/apache-tez/ http://dongxic ...
- python基础之类的编码风格
自定义工作流程: 一开始应让代码结构尽可能简单.先尽可能在一个文件中完成所有的工作,确定一切都能正确运行后,再将类移动独立的模块中.如果你喜欢模块和文件的交互方式,可在项目开始时就尝试将类存储到模块中 ...
- tomcat和jboss的区别
1. Tomcat是Apache鼎力支持的Java Web应用服务器(注:servlet容器),由于它优秀的稳定性以及丰富的文档资料,广泛的使用人群,从而在开源领域受到最广泛的青睐. 2. Jboss ...
- window下安装mysql详细步骤
1.下载安装包 打开mysql官网下载页面:http://dev.mysql.com/downloads/mysql/ 1.选择相应的版本和平台 2.mysql配置 打开刚刚解压的文件夹F:\mysq ...
- 系列文章(三):WAPI为无线局域网WLAN安全而生——By Me
导读:无线局域网(又称为WLAN,Wireless Local Area Network),其应用领域不断拓展,无线接入所具有的前所未有的连接性和自动化能够为人们带来巨大的便利和商机.与此同时,在信息 ...
- Deeplearning——动态图 vs. 静态图
动态图 vs. 静态图 在 fast.ai,我们在选择框架时优先考虑程序员编程的便捷性(能更方便地进行调试和更直观地设计),而不是框架所能带来的模型加速能力.这也正是我们选择 PyTorch 的理由, ...
- android studio本地gradle
1.从网站上下载http://services.gradle.org/distributions/ 2.打开工程里的gradle-wrapper.properties, distributionUrl ...
- jvm堆配置参数
1.-Xms初始堆大小默认物理内存的1/64(<1GB)(官方建议)2.-Xmx最大堆大小默认物理内存的1/4(<1GB)(官方建议),实际中建议不大于4GB3.一般建议设置 -Xms=- ...