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. SharePoint BI

    本篇博客主要针对SharePoint BI整体结构进行整理,为读者分析几种Sharepoint BI场景 先附一张自己做的结构图:

  2. Java利用dom4j生成xml文件、解析XML

    package com.fq.fanqi; import java.io.File;import java.io.FileWriter;import java.io.IOException;impor ...

  3. Trifo-VIO:Roubst and Efficient Stero Visual Inertial Odometry using Points and Lines论文笔记

    这是2018-IROS上的一篇文章,亮点是作者提出了Lines特征的VIO方案,还有就是提出一个新颖的回环检测,不是用传统的基于优化的方法或者BA,另外作者还发布了一个新的用于VIO的数据集.亮点主要 ...

  4. sqli_labs第一关

    安装 从https://github.com/Audi-1/sqli-labs下载源代码 搭建环境用的是phpstudy 编辑sqli\sql-connections\db-creds.inc文件 修 ...

  5. mysql_系统数据库认识

    show databases:查看mysql自带数据库有information_schema,mysql, performance_schema, test information_schema数据库 ...

  6. iOS接收远程通知响应方法

    点击 iOS 接收远程推送主要牵扯到的方法有以下五种 (1) - (BOOL)application:(UIApplication *)application didFinishLaunchingWi ...

  7. IIS 部署WCF时遇到这么个错:

    转(http://blog.csdn.net/vic0228/article/details/48806405) 部署WCF时遇到这么个错: "The service cannot be a ...

  8. centos7 docker镜像加速器配置

    CentOS的配置方式略微复杂,需要先将默认的配置文件复制出来 /lib/systemd/system/docker.service -> /etc/systemd/system/docker. ...

  9. CentOS7.1 KVM虚拟化之linux虚拟机安装(2)

    一.上传ISO文件到/data/iso下 这里使用CentOS-5.5-i386-bin-DVD.iso 二.安装CentOS5.5 CentOS7.1 安装KVM虚拟机默认磁盘格式为qcow2(推荐 ...

  10. Keras网络层之“关于Keras的层(Layer)”

    关于Keras的“层”(Layer) 所有的Keras层对象都有如下方法: layer.get_weights():返回层的权重(numpy array) layer.set_weights(weig ...