Buy the souvenirs - HDU 2126 背包dp

本博客探讨了如何在有限预算下购买最多种类的纪念品,并提供了使用动态规划算法解决此类问题的方法。

Buy the souvenirs

Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 915    Accepted Submission(s): 311


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

题意:买最多数量的物品有多少种买法。

思路:dp[i][j]表示买i种物品花j元钱的情况是否存在。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[31][510],n,m;
void solve()
{ int i,j,k;
  for(i=n;i>=1;i--)
  { k=0;
    for(j=m;j>=0;j--)
     k+=dp[i][j];
    if(k>0)
    { printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",k,i);
      return;
    }
  }
  printf("Sorry, you can't buy anything.\n");
}
int main()
{ int t,i,j,k,p;
  scanf("%d",&t);
  while(t--)
  { scanf("%d%d",&n,&m);
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(i=1;i<=n;i++)
    { scanf("%d",&p);
      for(j=m;j>=p;j--)
       for(k=i;k>=1;k--)
        if(dp[k-1][j-p]>0)
         dp[k][j]+=dp[k-1][j-p];
    }
    solve();
  }
}





A. 纪念品 (Souvenir) A C D E F G H 传统题 1000ms 256MiB Description Xiaowei suddenly acquires a superpower. He knows the daily prices of N N souvenirs for the next T T days. The price of a souvenir refers to the number of gold coins required to buy the souvenir or the number of gold coins that can be earned by selling a souvenir. Every day, Xiaowei can carry out the following two types of transactions any number of times: Choose a souvenir. If he has enough gold coins with him, buy the souvenir at its price for that day. Sell any souvenir he has and earn gold coins equal to the price of the souvenir for that day. The gold coins earned by selling souvenirs on a day can be used to buy souvenirs on the same day too. Similarly, souvenirs purchased on a day can also be sold for gold coins on the same day. After T T days, Xiaowei’s superpower will disappear. Therefore, he will sell all the souvenirs he still has on the T T-th day. Currently, Xiaowei has M M gold coins, and he wants to have as many gold coins as possible after the superpower disappears. What is the maximum number of gold coins he can have? Input Format The first line contains three positive integers T T, N N and M M - the number of days for which Xiaowei knows souvenir prices, the number of souvenirs, and the number of gold coins that Xiaowei currenly has. i i-th of the next T T lines contains N N positive integers P i , 1 , P i , 2 , . . . , P i , N , P i,1 ​ ,P i,2 ​ ,...,P i,N ​ , where P i , j P i,j ​ denotes the price of the j j-th souvenir on the i i-th day. Output Format Print one line containing a positive integer denoting the maximum number of gold coins Xiaowei can have after his superpower disappears. 输入数据 1 6 1 100 50 20 25 20 25 50 输出数据 1 305 The best strategy is: On the second day, spend all 100 100 gold coins to buy 5 5 units of souvenir 1 1. Sell all the 5 5 units of souvenir 1 1 on the third day and get 125 125 gold coins. Buy 6 6 units of souvenir 1 1 on the fourth day. 5 5 gold coins are left. On the sixth day, sell all souvenirs for 300 300 gold coins. On the fourth day there were 5 5 gold coins remaining, so now there are 305 305 gold coins remaining. After the superpower disappears, Xiaowei has 305 305 gold coins, which is the maximum possible. 输入数据 2 3 3 100 10 20 15 15 17 13 15 25 16 输出数据 2 217 The best strategy is: On the first day, spend all the gold coins to buy 10 10 units of souvenir 1 1. Sell all the units of souvenir 1 1 on the next day and get 150 150 gold coins. Buy 8 8 units of souvenir 2 2 and 1 1 unit of souvenir 3 3. 1 1 gold coin is remaining. On the third day, sell all souvenirs and get 216 216 gold coins. On the second day, there was 1 1 gold coin left, so in total there are 217 217 gold coins left. After the superpower disappears, Xiaowei has 217 217 gold coins, which is the maximum possible. Constraints For 10 % 10% of the data, T = 1 T=1. For 30 % 30% of the data, T ≤ 4 , N ≤ 4 , M ≤ 100 , 10 ≤ P i , j ≤ 1001 T≤4,N≤4,M≤100,10≤P i,j ​ ≤1001. For another 15 % 15% of the data, T ≤ 100 , N = 1 T≤100,N=1. For another 15 % 15% of the data, T = 2 , N ≤ 100 T=2,N≤100. For 100 % 100% of the data, T ≤ 100 , N ≤ 100 , M ≤ 10 3 , 1 ≤ P i , j ≤ 10 4 T≤100,N≤100,M≤10 3 ,1≤P i,j ​ ≤10 4 . It is guaranteed that at any time, the number of gold coins that Xiaowei has can't exceed 10 4 10 4 .
最新发布
10-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值