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. Java正则表达式匹配例子

    Java正则表达式匹配例子 package com.ibm.test; import java.util.regex.Matcher; import java.util.regex.Pattern; ...

  2. ZOJ2929 Penalty Kick(概率)

    题目挺水的,但由于其独特的阅读量比赛的时候没发现这道水题,在此做一下翻译,如果有人搜到这翻译的话有帮助的话自然最好啦. 中国队平局进入最后的点球决胜局,首先抛硬币决定谁先罚球,然后先是罚五球,如果罚的 ...

  3. HDU1542 Atlantis(矩形面积并)

    #pragma warning (disable:4996) #include<iostream> #include<cstring> #include<string&g ...

  4. POJ 2017

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("t ...

  5. Codeforces Round #335 (Div. 2) A. Magic Spheres 模拟

    A. Magic Spheres   Carl is a beginner magician. He has a blue, b violet and c orange magic spheres. ...

  6. 使用secureCRT远程Linux,出现远程主机拒绝连接

    1.查看是否开启远程连接, 控制面板->程序和功能->打开或关闭windows功能->勾选telnet服务器和telnet客户端2.cmd命令行输入telnet ip地址 端口号(比 ...

  7. Project Euler 102:Triangle containment 包含原点的三角形

    Triangle containment Three distinct points are plotted at random on a Cartesian plane, for which -10 ...

  8. PHP 反射机制Reflection

    简介 PHP Reflection API是PHP5才有的新功能,它是用来导出或提取出关于类.方法.属性.参数等的详细信息,包括注释. class Reflection { } interface R ...

  9. servlet学习笔记三

    Servlet主要内容: 1)状态跟踪 一.状态跟踪 HTTP协议是无状态协议,即请求与请求之间没有任何关系,也就是不会记住任何数据. 但若想在请求间传递数据,怎么办?web里的三个基本容器对象可以解 ...

  10. jsp中利用checkbox进行批量删除

    一.将前台jsp页面中的所有你要用到checkbox的name值设为相同,如 <input type="checkbox" name="userid"&g ...