hdu 2126 背包种类

Buy the souvenirs

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


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.
 

Author
wangye
 


题意:n个物品,m元钱,每个物品最多买一次,问最多可以买几件物品,并且输出方案数。加一维表示已经买几件物品。

我这里用了一个数组dp[v][2] ,dp[v][0] 储存原本要记录的 dp ,而 dp[v][1] 则记录方案数目。记住需要初始化,把所有 dp[i][1] 都设置成1。


View Code

Problem : 2126 ( Buy the souvenirs )     Judge Status : Accepted
RunId : 17026131    Language : G++    Author : 1136242673
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<cctype>
#define max(a,b)(a>b?a:b)
#define min(a,b)(a<b?a:b)
#define INF 0x3f3f3f3f

using namespace std;
int dp[550][2]; ///dp[v][0] 储存原本要记录的 dp ,而 dp[v][1] 则记录方案数目
int p[50];

int main()
{
    int t,n,m,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
            scanf("%d",&p[i]);

        memset(dp,0,sizeof(dp));
        for(i=0;i<m;i++)
            dp[i][1]=1;
        for(i=0;i<n;i++)
        {
            for(j=m;j>=p[i];j--)
            {
                if(dp[j][0]==dp[j-p[i]][0]+1)
                    dp[j][1]=dp[j-p[i]][1]+dp[j][1];
                else if(dp[j][0]<dp[j-p[i]][0]+1)
                {
                    dp[j][0]=dp[j-p[i]][0]+1;
                    dp[j][1]=dp[j-p[i]][1];
                }
            }
        }
        if(dp[m][0]!=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;
}

还有其他方法 见大神    http://blog.csdn.net/crazy_ac/article/details/7869411


背包的一种进化版 , 除了记录最多能买多少个 , 需要记录买这么多个的方法 , 所以要在二维的基础上加多一维 .

状态转移方程如下 : f[i][j][k]=f[i-1][j-t[i]][k-1]+f[i-1][j][k];

( 前 i 个物品在有 j 元的时候买 k 个物品的方法 ,t[i] 为第 i 个物品的价格 )

 

 

代码如下 :

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. int i,j,m,n,p,t[31],k,maxxx,a,b,s,g;  
  4. int f[31][501][431];  
  5. int main()  
  6. {  
  7.     scanf("%d",&p);  
  8.     for (g=1;g<=p;++g)  
  9.     {  
  10.         scanf("%d%d",&n,&m);  
  11.         maxxx=0;  
  12.         for (i=1;i<=n;++i)   
  13.             scanf("%d",&t[i]);  
  14.         for (i=1;i<=n;++i)  
  15.             for (j=1;j<=m;++j)  
  16.                 for (k=1;k<=i;++k)  
  17.                     if (j>=t[i])  
  18.                     {  
  19.                         if (k==1) f[i][j][k]=f[i-1][j][k]+1;//由于k==0时没有进行初始化,所以当k==1时需要另外处理  
  20.                         else   
  21.                             f[i][j][k]+=(f[i-1][j-t[i]][k-1]+f[i-1][j][k]);  
  22.                     }  
  23.                     else f[i][j][k]=f[i-1][j][k];//当第i个物品买不了的时候,f[i][j][k]的值就是f[i-1][j][k]的值  
  24.                         for (i=1;i<=n;++i) if (f[n][m][i]!=0) maxxx=i;//求最大购买物品数maxxx  
  25.                         if (maxxx!=0) printf("You have %d selection(s) to buy with %d kind(s) of souvenirs./n",f[n][m][maxxx],maxxx);  
  26.                         else printf("Sorry, you can't buy anything./n");  
  27.     }  
  28.       
  29.     return 0;  
  30. }  

 

 

 

此算法的时间复杂度为 O(n*n*m), 虽然复杂度不高 , 不过无论是时间还是空间还是有很大的优化余地 .

 

另一种算法 (O(n*m)):

在二维背包的基础上加多一维只有 0 和 1 两个下标的第三维 , 其中 f[i][j][0] 表示 j 元在前 i 个物品中最多能买多少个物品 ,f[i][j][1] 表示买最多物品的方法数 .

状态转移方程为 :(v[i]为第 i 个物品的价格 )

f[i][j][0]=max(f[i-1][j][0],f[i-1][j-v[i]][0]+1);

if  (f[i-1][j][0]==f[i-1][j-v[i]][0]) f[i][j][1]= f[i-1][j-v[i]][1]+f[i-1][j][1];

else  if  (f[i-1][j][0]>f[i-1][j-v[i]][0]) f[i][j][1]=f[i-1][j][0];

else  f[i][j][1]= f[i-1][j-v[i]][1];

 

 

代码如下 :

[c-sharp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. int p,n,m,i,j,f[31][501][2],v[31];  
  4. int main()  
  5. {  
  6.     scanf("%d",&p);  
  7.     while (p--)  
  8.     {  
  9.         scanf("%d%d",&n,&m);  
  10.         for (i=1;i<=n;++i) scanf("%d",&v[i]);  
  11.         for (i=0;i<=n;++i) f[i][0][1]=1;  
  12.         for (j=0;j<=m;++j) f[0][j][1]=1;  
  13.         for (i=1;i<=n;++i)  
  14.             for (j=1;j<=m;++j)  
  15.                 if (j>=v[i])  
  16.                 {  
  17.                     if (f[i-1][j-v[i]][0]+1>f[i-1][j][0])  
  18.                     {  
  19.                         f[i][j][0]=f[i-1][j-v[i]][0]+1;  
  20.                         f[i][j][1]=f[i-1][j-v[i]][1];  
  21.                     }  
  22.                     else   
  23.                         if (f[i-1][j-v[i]][0]+1<f[i-1][j][0])  
  24.                         {  
  25.                             f[i][j][0]=f[i-1][j][0];  
  26.                             f[i][j][1]=f[i-1][j][1];  
  27.                         }  
  28.                         else   
  29.                         {  
  30.                             f[i][j][0]=f[i-1][j-v[i]][0]+1;  
  31.                             f[i][j][1]=f[i-1][j-v[i]][1]+f[i-1][j][1];  
  32.                         }  
  33.                 }  
  34.                 else   
  35.                 {  
  36.                     f[i][j][0]=f[i-1][j][0];  
  37.                     f[i][j][1]=f[i-1][j][1];  
  38.                 }  
  39.                 if (f[n][m][0]) printf("You have %d selection(s) to buy with %d kind(s) of souvenirs./n",f[n][m][1],f[n][m][0]);  
  40.                 else printf("Sorry, you can't buy anything./n");  
  41.     }  
  42.     return 0;  
  43. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值