HDU2126——背包DP(开状态)——Buy the souvenirs

Description

When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes the travelers will buy some ones with pleasure. Not only can they give the souvenirs to their friends and families as gifts, but also can the souvenirs leave them good recollections. All in all, the prices of souvenirs are not very dear, and the souvenirs are also very lovable and interesting. But the money the people have is under the control. They can’t buy a lot, but only a few. So after they admire all the souvenirs, they decide to buy some ones, and they have many combinations to select, but there are no two ones with the same kind in any combination. Now there is a blank written by the names and prices of the souvenirs, as a top coder all around the world, you should calculate how many selections you have, and any selection owns the most kinds of different souvenirs. For instance:

And you have only 7 RMB, this time you can select any combination with 3 kinds of souvenirs at most, so the selections of 3 kinds of souvenirs are ABC (6), ABD (7). But if you have 8 RMB, the selections with the most kinds of souvenirs are ABC (6), ABD (7), ACD (8), and if you have 10 RMB, there is only one selection with the most kinds of souvenirs to you: ABCD (10).

Input

For the first line, there is a T means the number cases, then T cases follow. 
In each case, in the first line there are two integer n and m, n is the number of the souvenirs and m is the money you have. The second line contains n integers; each integer describes a kind of souvenir. 
All the numbers and results are in the range of 32-signed integer, and 0<=m<=500, 0<n<=30, t<=500, and the prices are all positive integers. There is a blank line between two cases.

Output

If you can buy some souvenirs, you should print the result with the same formation as “You have S selection(s) to buy with K kind(s) of souvenirs”, where the K means the most kinds of souvenirs you can buy, and S means the numbers of the combinations you can buy with the K kinds of souvenirs combination. But sometimes you can buy nothing, so you must print the result “Sorry, you can‘t buy anything.”

Sample Input

2
4 7
1 2 3 4

4 0
1 2 3 4

Sample Output

You have 2 selection(s) to buy with 3 kind(s) of souvenirs.

Sorry, you can‘t buy anything.

大意:给你n个物品你有m块钱,问最多能买几种物品,并且这最多的这种有多少种情况可以选择,

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int dp[550][2];
    int T;
    int w[550];
    int n,m;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i = 1; i <= n ; i++)
            scanf("%d",&w[i]);
        memset(dp,0,sizeof(dp));
        for(int i = 0 ; i <= m ; i++)
            dp[i][1] = 1;
        for(int i = 1; i <= n ; i++){
            for(int j = m ; j >= w[i]; j--){
                if(dp[j][0] == dp[j-w[i]][0] + 1){//dp[j][0]指用了j钱时的买的礼物数目,如果用j钱买的礼物数目和用不买i物品时所用的钱的买的礼物数目差1,说明用j钱买的礼物的种类就是这两者之和
                    dp[j][1] = dp[j][1] + dp[j-w[i]][1];//
                }
                else if(dp[j][0] < dp[j-w[i]][0] + 1){//如果不大于,说明dp[j-w[i]][0]优于dp[j][0]
                    dp[j][1] = dp[j-w[i]][1];
                    dp[j][0] = dp[j-w[i]][0] + 1;
                }
            }
        }
        if(dp[m][0])
            printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",dp[m][1],dp[m][0]);
        else printf("Sorry, you can‘t buy anything.\n");
    }
    return 0;
}

时间: 2024-10-15 09:21:44

HDU2126——背包DP(开状态)——Buy the souvenirs的相关文章

(01背包)HDU - 2126 Buy the souvenirs

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

[bzoj1775][Usaco2009 Dec]Vidgame 电视游戏问题_背包dp

1775: [Usaco2009 Dec]Vidgame 电视游戏问题 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1775 题解: 发现是个$zz$分组背包. 但是,正常的分组背包是,完全背包+01背包,在这里根本行不通因为数据范围. 故此我们考虑背包$dp$. 状态:$f_{(i,j)}$表示前$i$组,$i$选,花费$j$的最大价值:$g_{(i,j)}$表示$i$不选. 因为空间开的下,所以我们可以把$i$也放进去. 不然$

hdu2126 Buy the souvenirs

Problem Description When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes the travelers will buy some ones with pleasure. Not only can they give the souvenirs to their friends

POJ1787——背包DP(特定状态+回溯)——Charlie&#39;s Change

Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given

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背包)Buy the souvenirs (hdu 2126)

http://acm.hdu.edu.cn/showproblem.php?pid=2126 Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1904    Accepted Submission(s): 711 Problem Description When the winter holiday

Buy the souvenirs (01背包,动归)

When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souvenirs to sell, and sometimes the travelers will buy some ones with pleasure. Not only can they give the souvenirs to their friends and families as gift

【日常学习】【背包DP】codevs1155 金明的预算方案题解

题目来源:2006NOIPTG 题目描述 Description 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行".今天一早,金明就开始做预算了,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子: <dl><dd> <colgroup><col width="

BZOJ 1042 硬币购物(完全背包+DP)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1042 题意:给出四种面值的硬币c1,c2,c3,c4.n个询问.每次询问用d1.d2.d3.d4个相应的硬币能够拼出多少种总和为s? 思路:(1)首先,用完全背包求出f[i]表示四种硬币的数量无限制拼出i的方案数. (2)接着我们来理解 x=f[s]-f[s-(d1+1)*c1]的含义:x表示c1硬币的数量不超过d1个而其他三种硬币的数量不限制拼成s的方案数.我们举着例子来说明, 假设