HDU 2126 (背包方法数) Buy the souvenirs
DP还有很长很长一段路要走。。
题意:给出n纪念品的价格和钱数m,问最多能买多少件纪念品和买这些数量的纪念品的方案数。
首先,求能买最多的纪念品的数量,用贪心法可以解决。将价钱排序,然后从最便宜的开始买,这样就很容易求得最多买的纪念品的数量。
方案数就要用到动态规划。
dp[j][k]表示花费不超过j元买k件物品的方案数
dp[j][k] += dp[j-a[i]][k-1]
因为这里本来是个三维数组的,多一个维度用来表示前i件物品。调整了循环顺序,类似01背包空间上的优化,所以倒着循环就可以利用之前的计算结果节省空间。
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int dp[][], a[]; int main(void)
{
#ifdef LOCAL
freopen("2126in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
int n, m, num = , sum = ;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
scanf("%d", &a[i]); sort(a+, a++n);
for(int i = ; i <= n; ++i)
{
sum += a[i];
if(m >= sum)
++num;
}
if(num == n)
{
printf("You have 1 selection(s) to buy with %d kind(s) of souvenirs.\n", num);
continue;
}
if(num == )
{
printf("Sorry, you can't buy anything.\n");
continue;
}
memset(dp, , sizeof(dp));
for(int i = ; i <= m; ++i)
dp[i][] = ;
for(int i = ; i <= n; ++i)
for(int j = m; j >= a[i]; --j)
for(int k = num; k >= ; --k)
dp[j][k] = dp[j][k] + dp[j-a[i]][k-]; if(dp[m][num] == )
printf("Sorry, you can't buy anything.\n");
else
printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", dp[m][num], num);
}
return ;
}
代码君
解法二:
看注释吧。。
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; int a[];
int dp[][][]; //dp[i][j][0]表示前i件物品花费不超过j元
//能最多买的礼物数,dp[i][j][1]表示方案数 int main(void)
{
#ifdef LOCAL
freopen("2126in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
scanf("%d", &a[i]);
memset(dp, , sizeof(dp));
//边界初始化
for(int i = ; i <= n; ++i)
dp[i][][] = ;
for(int j = ; j <= m; ++j)
dp[][j][] = ;
for(int i = ; i <= n; ++i)
{
for(int j = ; j <= m; ++j)
{
if(j < a[i] || dp[i-][j-a[i]][]+ < dp[i-][j][])
{//如果买第i件礼物礼物总数变少,那么肯定不买!
dp[i][j][] = dp[i-][j][];
dp[i][j][] = dp[i-][j][];
}
else if(dp[i-][j-a[i]][] + == dp[i-][j][])
{//买的礼物数相同,则方案数为买和不买第i件礼物的方案数的总和
dp[i][j][] = dp[i-][j][];
dp[i][j][] = dp[i-][j][] + dp[i-][j-a[i]][];
}
else //dp[i-1][j-a[i]][0] + 1 > dp[i-1][j][0]
{//可以买更多数量的礼物
dp[i][j][] = dp[i-][j-a[i]][] + ;
dp[i][j][] = dp[i-][j-a[i]][];
}
}
} if(dp[n][m][])
printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", dp[n][m][], dp[n][m][]);
else
printf("Sorry, you can't buy anything.\n");
}
return ;
}
代码君
解法二空间上的优化之滚动数组:
对了,那个else不能去掉!血一般的教训
#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; int a[];
int dp[][]; int main(void)
{
#ifdef LOCAL
freopen("2126in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
scanf("%d", &a[i]);
memset(dp, , sizeof(dp));
for(int i = ; i <= m; ++i) //处理边界
dp[i][] = ;
for(int i = ; i <= n; ++i)
{
dp[][] = ;
for(int j = m; j >= a[i]; --j)
{
if(dp[j-a[i]][] + > dp[j][])
{
dp[j][] = dp[j-a[i]][] + ;
dp[j][] = dp[j-a[i]][];
}
else if(dp[j-a[i]][] + == dp[j][])
{
dp[j][] = dp[j][] + dp[j-a[i]][];
}
}
}
if(dp[m][])
printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", dp[m][], dp[m][]);
else
printf("Sorry, you can't buy anything.\n");
}
return ;
}
代码君
最后吐槽一下我发现的一个奇怪的现象:如果源代码的文件名里带括号,这样下断点调试的时候会自动退出的。CFree-5和DevCpp都是这样的,=_=||
HDU 2126 (背包方法数) Buy the souvenirs的更多相关文章
- (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背包)
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 01背包(求方案数)
Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 2157 How many ways??:矩阵快速幂【i到j共经过k个节点的方法数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2157 题解: 给你一个有向图,n个节点m条边,问你从i到j共经过k个节点的方法数(不算i点). 题解: ...
- [程序员代码面试指南]递归和动态规划-换钱的方法数(DP,完全背包)
题目描述 给定arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim,求组成aim的方法数. 解题思路 完全背包 和"求换钱的 ...
- HDU-2126 Buy the souvenirs
数组01背包. http://acm.hdu.edu.cn/showproblem.php?pid=2126 http://blog.csdn.net/crazy_ac/article/details ...
- hdu2126 Buy the souvenirs
Problem Description When the winter holiday comes, a lot of people will have a trip. Generally, ther ...
随机推荐
- wifidog源码分析 - 用户连接过程
引言 之前的文章已经描述wifidog大概的一个工作流程,这里我们具体说说wifidog是怎么把一个新用户重定向到认证服务器中的,它又是怎么对一个已认证的用户实行放行操作的.我们已经知道wifidog ...
- Unity3D战争迷雾效果
原地址:http://liweizhaolili.blog.163.com/blog/static/16230744201431835652233/ 最近一直都在做Flash相关的东西,很久没有空搞U ...
- POJ 3978 Primes(素数筛选法)
题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...
- 详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别 http://blog.sina.com.cn/s/blog_686999de0100jgda.html 实例: ...
- JavaWeb-Eclipse的下载和安装
Eclipse下载地址:http://www.eclipse.org/downloads/ Eclipse集成JDK 遇见弹框: 1.这是由于缺少JRE所导致的,Eclipse中带有自己的编译器,因此 ...
- Javascript Arguments,calle,caller,call,apply
一.Arguments 该对象代表正在执行的函数和调用他的函数的参数. [function.]arguments[n] 参数function :选项.当前正在执行的 Function 对象的名字. n ...
- MySQL: InnoDB 还是 MyISAM? (转载)
MyISAM存储引擎 原文作者:http://www.cnblogs.com/villion/archive/2009/07/09/1893762.html MyISAM是 默认存储引擎.它基于更老的 ...
- CF 197 DIV2 Xenia and Bit Operations 线段树
线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...
- VS2010中打开VS2013/VS2012项目
VS2010中打开VS2013/VS2012项目 (2014-04-03 23:47:53) 转载▼ 分类: IT VS低版本打开高版本创建的项目时会提示"选择的文件是解决方案文件,但是 ...
- lintcode:整数排序||
题目 给一组整数,按照升序排序.使用归并排序,快速排序,堆排序或者任何其他 O(n log n) 的排序算法. 解题 归并排序 public class Solution { /** * @param ...