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的更多相关文章

  1. (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 ...

  2. hdu 2126 Buy the souvenirs 二维01背包方案总数

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. hdu 2126 Buy the souvenirs(记录总方案数的01背包)

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. 【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 ...

  5. HDU 2126 01背包(求方案数)

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. HDU 2157 How many ways??:矩阵快速幂【i到j共经过k个节点的方法数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2157 题解: 给你一个有向图,n个节点m条边,问你从i到j共经过k个节点的方法数(不算i点). 题解: ...

  7. [程序员代码面试指南]递归和动态规划-换钱的方法数(DP,完全背包)

    题目描述 给定arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim,求组成aim的方法数. 解题思路 完全背包 和"求换钱的 ...

  8. HDU-2126 Buy the souvenirs

    数组01背包. http://acm.hdu.edu.cn/showproblem.php?pid=2126 http://blog.csdn.net/crazy_ac/article/details ...

  9. hdu2126 Buy the souvenirs

    Problem Description When the winter holiday comes, a lot of people will have a trip. Generally, ther ...

随机推荐

  1. ASP.NET MVC 应用提速的十种方法

    [编者按]本文作者为 DZone 社区的最具价值博主(MVB) Jonathan Danylko,主要介绍为 ASP.NET MVC 应用提速的十种方法.由国内 ITOM 管理平台 OneAPM 编译 ...

  2. ZOJ3560 Re:the Princess(高斯消元法)

    题目要读很久才能理解它的意思和笑点(如果你也看过那个笑话的话),读懂之后就会发现是一个高斯消元法的题目,对于我来说难点不在高斯消元,而在于字符串处理.先来说说题意吧: 总共有n个人,n个人都会有一段话 ...

  3. iOS多线程的初步研究(四)-- NSTimer

    理解run loop后,才能彻底理解NSTimer的实现原理,也就是说NSTimer实际上依赖run loop实现的. 先看看NSTimer的两个常用方法: + (NSTimer *)timerWit ...

  4. javascript 图片延迟加载

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  5. procedure can't return a result set in the given context

    调用存储过程失败!出现如下错误:PROCEDURE ipbx.qu_ery can't return a result set in the given context, ipbx是数据库, qu_e ...

  6. easyUI Admin 模板

    http://www.oschina.net/p/Easy-Admin?fromerr=23Tfbale

  7. 15 things to talk about in a healthy relationship

    15 things to talk about in a healthy relationship男女交往中可以谈论的15个话题 1. Your Daily Activities 1. 你的日常活动 ...

  8. 【Spring开发】—— Spring Core

    原文:[Spring开发]-- Spring Core 前言 最近由于一些工作的需要,还有自己知识的匮乏再次翻开spring.正好整理了一下相关的知识,弥补了之前对spring的一些错误认知.这一次学 ...

  9. 屏幕实战效果解析:IPS/TFT/AMOLED/SLCD

    现在手机市场上,智能手机种类繁多,手机屏幕材质也是五花八门.对于一般消费者来说,一款手机是否值得购买,除了关心它的硬件参数以外,更重要的一点就是看它的屏幕.除了屏幕尺寸以外,影响着大家对该手机的第一感 ...

  10. list、set、map的特点

    java 集合(list.set.map)的特点 集合相关的类有一大堆,一般也只用到常用的方法增删改查,而且它它们的方法名也基本一样,所以一直都不知道什么时候用什么集合, 今天趁有空特意从网上整理资料 ...