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. mysql死锁,等待资源,事务锁,Lock wait timeout exceeded; try restarting transaction解决

    前面已经了解了InnoDB关于在出现锁等待的时候,会根据参数innodb_lock_wait_timeout的配置,判断是否需要进行timeout的操作,本文档介绍在出现锁等待时候的查看及分析处理: ...

  2. Sqli-labs less 58

    Less-58 执行sql语句后,并没有返回数据库当中的数据,所以我们这里不能使用union联合注入,这里使用报错注入. Payload:http://127.0.0.1/sqli-labs/Less ...

  3. delphi 中几种多线程操作方式

    在了解多线程之前我们先了解一下进程和线程的关系 一个程序至少有一个主进程,一个进程至少有一个线程. 为了保证线程的安全性请大家看看下面介绍 Delphi多线程同步的一些处理方案大家可以参考:http: ...

  4. Acdream1217 Cracking' RSA(高斯消元)

    题意:给你m个数(m<=100),每个数的素因子仅来自于前t(t<=100)个素数,问这m个数的非空子集里,满足子集里的数的积为完全平方数的有多少个. 一开始就想进去里典型的dp世界观里, ...

  5. HDU5002 Tree(LCT)

    今天做了一道LCT模板题之后忽然间好像记起来LCT的模板怎么用了,于是就把上次网络赛的一道LCT补一下.典型的删边,加边操作,还有路径加和路径set为一个数.维护的是路径第二大以及它有多少个,后来想想 ...

  6. HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  7. iOS开发--自动布局

    距离左边的: 距离顶部的: 距离右边的: 距离底部的:

  8. JavaPersistenceWithHibernate第二版笔记Getting started with ORM-001用JPA和Hibernate实现HellowWorld(JTA、Bitronix)

    一.结构 二.model层 1. package org.jpwh.model.helloworld; import javax.persistence.Entity; import javax.pe ...

  9. 将EXE作为资源,然后在释放到磁盘上并运行该exe程序(使用了FindResource,LoadResource,然后用CFile写成一个文件)

    // 将exe作为资源加入,然后再释放出来,并运行 try { HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_EXE1), _T(" ...

  10. UML系列01之 UML用例图

    UML,全称是Unified Modeling Language,中文是"统一建模语言".通俗点说,UML是一种创建模型的语言. UML是在开发阶段,说明,可视化,构建和书写一个面 ...