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元钱,问你最多能买个多少物品,并且有多少种解决方案. 一开始想到的是,先解 ...
随机推荐
- Less-mixin判断(守卫)二
mixin卫士--判断 或与且语法 且:()and() 或:(),() --且 examlpe: .test(@a) when (isNumber(@a)) and (@a>=5){ font- ...
- grunt的简单应用
grunt是干什么的呢,一句话:自动化.对于需要反复重复的任务,例如压缩(minification).编译.单元测试.linting等,自动化工具可以减轻你的劳动,简化你的工作.当你在 Gruntfi ...
- 转义字符的理解(JAVA、字符串和正则表达式)
一.原理总结: 要理解转义,首先要从正则表达式说起. 在正则表达式中:*和\是特殊字符:为了匹配这两个字符本身,正则表达式中需要写为\*和\\ 在Java中,只能用字符串表示正则表达式,所以需要把\* ...
- 一起做RGB-D SLAM调试
最近在学习高博的一起做RGB-D SLAM第一版本,其中调试出现了挺多问题,百度查找许多资料, 最后调通所有程序,记录以下运行环境. 高博一起做RGB-D SLAM系列主页: http://www.c ...
- 【Python之路】第十九篇--Python操作MySQL
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...
- php strcmp()字典排序
字典排序(lexicographical order)是一种对于随机变量形成序列的排序方法.其方法是,按照字母顺序,或者数字小大顺序,由小到大的形成序列. 比如,字典中a-z,是依次递增的,a,b,c ...
- 系统根据用户cookies,为用户打上各种标签
DSP营销学院_品友学院 | 品友推广官网 http://e.ipinyou.com/school_article40.html 智能算法+动态出价=最大发挥推广费用的价值 针对每一个曝光进行甄别和竞 ...
- Compilation failed: internal java compiler error
在Idea中编译时出现这个错误:Error:java: Compilation failed: internal java compiler error. Information:Using java ...
- 用户登录失败,该用户与可信SQL Server连接无关联,错误:18452
安装好SQLServer2005(或者装了Visual Studio 2008后自带的SQLServer2005)用SQL Server身份验证的登录的时候有时候会发生这种情况: 这样的错误的原因是: ...
- qt下通过socket传送中文
zz 1.在main函数里我之前就加了一句QTextCodec::setCodecForTr( QTextCodec::codecForLocale() ); 现在再加一句QTextCodec::se ...