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. server.xml文件详解

    一.server.xml文件介绍 1.server.xml作用     Server.xml配置文件用于对整个容器进行相关的配置. 2.server.xml文件的配置元素列表 <Server&g ...

  2. 170228、Linux操作系统安装ELK stack日志管理系统--(1)Logstash和Filebeat的安装与使用

    安装测试环境:Ubuntu 16.04.2 LTS 前言 (1)ELK是Elasticsearch,Logstash,Kibana 开源软件的集合,对外是作为一个日志管理系统的开源方案.它可以从任何来 ...

  3. mysql查询某周的起始日期和终止日期

    select subdate(curdate(),date_format(curdate(),'%w')-1) select subdate(curdate(),date_format(curdate ...

  4. 在java中public void与public static void有什么区别 ?

    public void 修饰是非静态方法,该类方法属于对象,在对象初始化(new Object())后才能被调用:public static void 修饰是静态方法,属于类,使用类名.方法名直接调用 ...

  5. Java日志记录工具SLF4J介绍

    SLF4J是什么 SLF4J是一个包装类,典型的facade模式的工具,对用户呈现统一的操作方式,兼容各种主流的日志记录框架,典型的有log4j/jdk logging/nop/simple/jaka ...

  6. DPM Server切换

    DPMserver切换: Dpm有2个volum:副本卷和恢复点卷 (1)首先在exchangeserver上面安装agent (2)在exchangeserver上指定dpmserver: cd&q ...

  7. mysql 数据库备 及移动当天数据到历史表里 window下可用

    1 数据移动到历史表中,同时删除当天数据 test-move-record.bat c: cd C:\FQ_ManageServer\mysqlback mysql.exe -h 10.71.1.23 ...

  8. pycharm中选择python interpreter

    pycharm中选择python interpreter pycharm中有两处地方需要选择python解释器: 一处是调试配置(edit configurations)处,这里选择python解释器 ...

  9. redis数据库设计(转)

    原文:http://segmentfault.com/q/1010000000316112 redis是什么 redis就是一个存储key-value键值对的仓库,如何使用redis在于如何理解你需要 ...

  10. 我的Android进阶之旅------>android中一些特殊字符(如:←↑→↓等箭头符号)的Unicode码值

    在项目中,有时候在一些控件(如Button.TextView)中要添加一些符号,如下图所示:                         这个时候可以使用图片的方式来显示,不过这些可以直接使用Un ...