hdu2126 Buy the souvenirs
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.
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背包,给你n个背包以及它们的重量,再给你背包的总重量m,让你求取得最大的物品个数是多少,共有几种方案。之前状态转移方程写错了错了好多次,后来改对了。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define inf 88888888
int w[50],dp[600][2];
int main()
{
	int n,m,i,j,T,num,num1,fas,minx,flag;
	scanf("%d",&T);
	while(T--)
	{
		minx=inf;
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++){
			scanf("%d",&w[i]);
			if(w[i]<minx)minx=w[i];
		}
		if(m<minx){
			printf("Sorry, you can't buy anything.\n");continue;
		}
		for(i=1;i<=n;i++){
			for(j=0;j<=m;j++){
				dp[j][0]=0;dp[j][1]=1;
			}
		}
		for(i=1;i<=n;i++){
			for(j=m;j>=w[i];j--){
				if(dp[j-w[i]][0]+1>dp[j][0]){
					dp[j][0]=dp[j-w[i]][0]+1;dp[j][1]=dp[j-w[i]][1];
				}
				else if(dp[j-w[i]][0]+1==dp[j][0]){
					dp[j][1]+=dp[j-w[i]][1];
				}
			}
		}
		printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",dp[m][1],dp[m][0]);
	}
	return 0;
}
hdu2126 Buy the souvenirs的更多相关文章
- HDU-2126                                Buy the souvenirs
		
数组01背包. http://acm.hdu.edu.cn/showproblem.php?pid=2126 http://blog.csdn.net/crazy_ac/article/details ...
 - HDU--2126 Buy the souvenirs(二维01背包)
		
题目http://acm.hdu.edu.cn/showproblem.php?pid=2126 分析:有两个要求,一是计算最多可以选多少中纪念品:而是计算选最多纪念品的方案有多少种, 即统计最优方案 ...
 - (01背包)Buy the souvenirs (hdu 2126)
		
http://acm.hdu.edu.cn/showproblem.php?pid=2126 Buy the souvenirs Time Limit: 10000/1000 MS (Java/Oth ...
 - 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背包)
		
When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souve ...
 - 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 (动态规划)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意:给你n个物品,m元钱,问你最多能买个多少物品,并且有多少种解决方案. 一开始想到的是,先解 ...
 - HDU 2126 Buy the souvenirs  (01背包,输出方案数)
		
题意:给出t组数据 每组数据给出n和m,n代表商品个数,m代表你所拥有的钱,然后给出n个商品的价值 问你所能买到的最大件数,和对应的方案数.思路: 如果将物品的价格看做容量,将它的件数1看做价值的话, ...
 - HDU 2126 (背包方法数) Buy the souvenirs
		
DP还有很长很长一段路要走.. 题意:给出n纪念品的价格和钱数m,问最多能买多少件纪念品和买这些数量的纪念品的方案数. 首先,求能买最多的纪念品的数量,用贪心法可以解决.将价钱排序,然后从最便宜的开始 ...
 
随机推荐
- Redis Cluster 集群节点维护 (三)
			
Redis Cluster 集群节点维护: 集群运行很久之后,难免由于硬件故障,网络规划,业务增长,等原因对已有集群进行相应的调整,比如增加redis nodes 节点,减少节点,节点迁移,更换服务器 ...
 - Linux Clone函数
			
Linux Clone函数 之前某一次有过一次面试,问了内核中是怎么创建命名空间的? 下面就来扒一扒clone的精髓,以及如何通过它创建命名空间. 目录 Linux Clone函数 使用clone创建 ...
 - Java 使用 mail.jar 实现邮件发送
			
目录 准备工作 使用到的 jar 包 实现代码 准备工作 要想实现邮件发送, 需要先打开发送邮箱的 POP3/SMTP 服务,打开方式在 设置>帐户 中去打开,打开之后如果是qq邮箱会获得一个授 ...
 - 【Java】面向对象 - 封装
			
继承 封装 多态 重新搞一波 复习巩固 简单记录 慕课网 imooc Java 零基础入门-Java面向对象-Java封装 封装 封装是什么? 将类的某些信息隐藏在类内部,不允许外部程序直接访问 通过 ...
 - linux搭建简单samba服务器
			
1.安装需要的软体 yum install -y samba samba-client samba-common 2.创建samba需要的本地用户,创建samba服务使用的目录 Linux系统文件的读 ...
 - SAP中数据库表长度的界定
			
SAP中,如何查看表和关键字的长度?通过SE11菜单栏Extras->table width 可以看到.然而SAP在系统也会将表分类,特别是在可扩展的表维护视图中,分为如下几类 ult ...
 - jQuery库 之 jquery slimscroll插件使用
			
1.引入jQuery插件 <script type="text/javascript" src="jquery.min.js"></scrip ...
 - 《进击吧!Blazor!》第一章 1.初识 Blazor
			
作者介绍 陈超超 Ant Design Blazor 项目贡献者 拥有十多年从业经验,长期基于.Net技术栈进行架构与开发产品的工作,Ant Design Blazor 项目贡献者,现就职于正泰集团 ...
 - Docker下梦织CMS的部署
			
摘要:Docker的广泛应用相对于传统的虚拟机而言提高了资源的利用率,推广后docker的影响不容忽视,在启动速度.硬盘.内存.运行密度.性能.隔离性和迁移性方面都有很大的提高.本次实训我们在cent ...
 - Django-html文件实例
			
1.实例1,登陆界面 <!DOCTYPE html> <head> <meta http-equiv="content-type" content=& ...