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 ...
随机推荐
- Ambient Occlusion
一般在光照模型中,ambient light的计算方法为:A = l * m,其中l表示表面接收到的来自光源的ambient light的总量,而m表示表面接收到ambient light后,反射和吸 ...
- C#创建UTF8无BOM文本文件
In order to omit the byte order mark (BOM), your stream must use a custom instance of UTF8Encoding i ...
- java集合之ArrayList的实现原理
1. ArrayList概述: ArrayList是List接口的可变数组的实现.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类还提供一些方法来操作内部 ...
- Ehcache使用
http://www.360doc.com/content/14/0423/17/16946725_371472946.shtml http://www.myexception.cn/web-appl ...
- C# 调用Windows API实现两个进程间的通信
使用Windows API实现两个进程间(含窗体)的通信http://blog.csdn.net/huangxinfeng/article/details/5513608 从C#下使用WM_COPYD ...
- IEEE 802.3 Ethernet
Introduction Ethernet 是过去30年以来最为成功的局域网(local area networking)技术. 1. First widely used LAN technology ...
- Log4net学习
转自:http://www.cnblogs.com/sirkevin/archive/2012/06/13/2548449.html Log4net简介根据日志类别保存到不同的文件,并按照日期生成不同 ...
- interviewbit :Min Steps in Infinite GridBookmark Suggest Edit
You are in an infinite 2D grid where you can move in any of the 8 directions : (x,y) to (x+1, y), (x ...
- lintcode:颜色分类
颜色分类 给定一个包含红,白,蓝且长度为 n 的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红.白.蓝的顺序进行排序. 我们可以使用整数 0,1 和 2 分别代表红,白,蓝. 样例 给你数组 ...
- android中使用html作布局文件
在android开发中,通常使用xml格式来描述布局文件.就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层.大部分企业,其实还是程序员自己动手布局.这样既浪费时间和精力,也未必 ...