hdu 2126 Buy the souvenirs 【输出方案数】【01背包】(经典)

题目链接:https://vjudge.net/contest/103424#problem/K

题目大意:

给n个物品,和m块钱,输出能够购买最多物品的个数和购买这么多物品的方案数。

#include <cstring>
#include <iostream>
#include <algorithm>
#define clr(a) (memset(a,0,sizeof(a)))
using namespace std;
int dp[510][50], a[50];
int n, m;
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d %d", &n, &m);
        for (int i = 1; i <= n; i++)scanf("%d", &a[i]);
        clr(dp);
        dp[0][0] = 1;
        int mx = 0;
        for (int i = 1; i <= n; i++)
            for (int j = m; j >= a[i]; j--)
            {
                for (int k = n - 1; k >= 0; k--)
                {
                    if (dp[j - a[i]][k])dp[j][k + 1] += dp[j - a[i]][k];
                    mx = max(mx, k + 1);             //mx为最多能买几件物品
                }
            }
        if (mx == 0)
        {
            puts("Sorry, you can‘t buy anything.");
            continue;
        }
        int ans = 0;           //ans为能够买mx件物品的选择方案总数(mx为最多能购买的物品数量)
        for (int i = 0; i <= m; i++)ans += dp[i][mx];
        printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", ans, mx);
    }
    return 0;
}

2018-05-21

原文地址:https://www.cnblogs.com/00isok/p/9069929.html

时间: 2024-08-11 05:42:17

hdu 2126 Buy the souvenirs 【输出方案数】【01背包】(经典)的相关文章

hdoj 2126 Buy the souvenirs 【另类01背包】

题意:求最多购买的件数以及有几种方法. 一看到这题就想到了背包,因为求得是种类数,所以我们可以将件数看做价值,将价格看做重量,这就变成01背包了(dp),但是还要求有几种购买方案,那么再来一个背包(kind). 分析:有三种情况: 1>dp[j] < dp[j-s[i]]+1 那么对于这一种情况  方案背包的状态转移方程是kind[j] = kind[j-s[i]]?kind[j-s[i]]:1;(考虑到kind[j-s[i]] ==0的时候,这时候kind[j] = 1): 证明:为什么是k

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

Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1662    Accepted Submission(s): 611 Problem Description When the winter holiday comes, a lot of people will have a trip. Genera

(01背包)HDU - 2126 Buy the souvenirs

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意:给n个物品,和m块钱,输出最多物品个数和其方案数. 委屈:求出最多物品个数就是一个裸的01背包,但是同时求出方案数,难住了我. 想了半天,感觉可以一波dp求出来,但是又想不明白状态是怎么表示和转移的. 无奈就先写个dfs提交试一发,果断超时了. 最后无奈看了题解,只能说,01背包还是不会. 其实与其说01背包不会不如说动态规划不会,感觉好难. 感觉自己好lowbee啊.. 感觉碰到dp就

hdu 2126 Buy the souvenirs 买纪念品(01背包,略变形)

题意:给出一些纪念品的价格,先算出手上的钱最多能买多少种东西k,然后求手上的钱能买k种东西的方案数.也就是你想要买最多种东西,而最多种又有多少种组合可选择. 思路:01背包.显然要先算出手上的钱m最多能买多少种东西k,可以从价格最少的纪念品买起,看最多能买多少种,置为k.接下来按照常规01背包计算,需要记录下方案数和组成的物品数,看代码就会懂的. 1 #include <iostream> 2 #include <stdio.h> 3 #include <cstring>

HDU 2126 Buy the souvenirs

Buy the souvenirs Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 212664-bit integer IO format: %I64d      Java class name: Main When the winter holiday comes, a lot of people will have a trip. Generally, the

[HDU 2126] Buy the souvenirs (动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 题意:给你n个物品,m元钱,问你最多能买个多少物品,并且有多少种解决方案. 一开始想到的是,先解决给m元钱因为我花的钱少就一定能购买够多的物品,因此是个贪心算法. 记买最多的物品数为c. 然后就是设计状态dp[i][j]代表我从前i个物品里花了j元钱,买c个物品有多少种方案. 后来发现状态维数不够,得重新想想. 于是就想到: 设计状态dp[i][j][k]代表我从前i个物品里买了j个,花的钱不

HDU 3033 I love sneakers! (DP 01背包+完全背包)

Problem Description After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store. There are several brands of sneakers that Iserlohn wan

hdu 3339 In Action(迪杰斯特拉+01背包)

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5102    Accepted Submission(s): 1696 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project t

HDU 5887 Herbs Gathering(搜索求01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做,但是需要一些剪枝,先按体积排序,优先考虑体积大的物品,这样剪枝会剪得多一些(当然按照性价比排序也是可以的). 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdi