动态背包问题(HDU 2546 饭卡, 钱币兑换问题,Bone Collector题解)

题目描述

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。

某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。

Input

多组数据。对于每组数据:

第一行为正整数n,表示菜的数量。n<=1000。

第二行包括n个正整数,表示每种菜的价格。价格不超过50。

第三行包括一个正整数m,表示卡上的余额。m<=1000。

n=0表示数据结束。

Output

对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。

Sample Input

1

50

5

10

1 2 3 2 1 1 2 3 2 1

50

0

Sample Output

-45

32

题意分析

题意:01背包问题的变种问题。既然卡可以在5元之前透支,那么首先,若余额在其之下,便可直接输出
其次我们应当选择最贵的饭菜在最后一次购买它。便可转化为一个 M-5空间的背包,将其存到最满。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
const int maxn=1005;
int w[maxn];
int a[maxn]; 
int main(int argc, char** argv) {
 int n;
 while(cin>>n&&n){
  int manx=0;
  int m;
  memset(w,0,sizeof(w));
  memset(a,0,sizeof(a));
  for(int i=0;i<n;i++){
   cin>>w[i];
   if(manx<w[i]){
    manx=w[i];
   }
  } 
  sort(w,w+n);
  cin>>m;
  if(m<5){
   cout<<m<<endl;
   continue;
  }
  for(int i=0;i<n-1;i++){
   for(int j=m-5;j>=w[i];j--){
    a[j]=max(a[j],a[j-w[i]]+w[i]);
   /* if(j==m-5){
    cout<<j-w[i]<<"---"<<a[j-w[i]]<<endl;
    }*/
   }
   //cout<<a[m-5]<<"+++"<<endl;
  } 
  //cout<<a[m-5]<<endl;
  cout<<m-a[m-5]-manx<<endl;
 }
 return 0;
}
                                                钱币兑换问题

在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法。请你编程序计算出共有多少种兑法。

Input

每行只有一个正整数N,N小于32768。

Output

对应每个输入,输出兑换方法数。

Sample Input

2934
12553

Sample Output

718831
13137761

先看代码,着重看以下递推式的循环
题目思路:
递推式循环哪里外层从1到3,代表三种硬币,同时这三种硬币的价值等于他们的序号。内层j代表的是j分钱,从i开始,为什么是从i开始呢?

这是因为j代表的是钱数,不能小于当前价值为i的硬币。

现在我们来简单模一下这个循环

i = 1时 你会发现 从dp[1] 到 dp[MAXN] 都是 1,没错,就是这样。这是因为i = 1时代表只能选1分的硬币,自然只有一种。

那么i = 2时这时dp数组就是递增的了,这是因为当前已经可以选2了,当然是钱数越多,种类数越多了。

然后及时 i = 3了,这是同 i= 2一样,dp数组继续递增,至于为什么,这样算就能得到正确答案。

我们来举个例子

比如需要用这三种硬币来凑够5分钱,那么有几种方法呢?

首先模拟一下递推你会发现dp[5] 经过这几次运算。

i = 1时 dp[5] = 1;

i = 2时 dp[5] = dp[5] + dp[3] = 1 + 2 = 3;

i = 3时 dp[5] = dp[5] + dp[2] = 3 + 2 = 5; 其中dp[2] and dp[3] 也在因为递推而值改变,所以就有前面的模拟结果。

正好 5 有 1 1 1 1 1, 2 1 1 1, 2 2 1, 3 1 1 , 3 2 这五种。

那么来分析一下

首先 1 1 1 1 1 这样肯定是一种了,这样正好对应 i = 1的情况。然后 2 1 1 1 和 2 2 1又是两种。对应于当 i = 2 时 此时还是dp[5] = dp[5] + dp[3]; 加上dp[5]是因为dp[5] 是之前的已经知道的种数,dp[3] 则是因为当前为 选的是2分的硬币,所以选上一个2,那么还剩3个1,三个一组和的种数就是dp[3], 接着你会发现dp[3]能由1分和2分的硬币组成的种类数在dp[5]之前已经计算过了,所以直接哪来用就好了。

最后就是 i = 3的情况了,大致同i = 2 的想法一样。 5 除去一个3就是 2, 剩下的2分钱可组成种数dp[2]已经不能用3分的硬币来增加了,此外dp[2]在之前的递推中也已经计算过了。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 32767+3; 
long long dp[maxn]; 
int main(int argc, char** argv) {
 int n;
 int m=3;
 memset(dp,0,sizeof(dp));
 dp[0]=1 ;
 for(int i=1;i<=3;i++){
  for(int j=i;j<maxn;j++){
   dp[j]=max(dp[j],dp[j]+dp[j-i]);
  }
 }
 while(cin>>n){
  cout<<dp[n]<<endl;
 }
 return 0;
}

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …

The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

Input

The first line contain a integer T , the number of cases.

Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output

One integer per line representing the maximum of the total value (this number will be less than 2 31).

Sample Input

1

5 10

1 2 3 4 5

5 4 3 2 1

Sample Output

14
两种解法:
可以用一维数组也可以用二位数组两种解法:
一维数组(个人倾向)dp[j]只需要吧 j 当成还剩余的空间(也就是还可以放的重量)dp[j]当成价值,主要是靠大家在题目中推出来,
也可以用二位数组:
用子问题定义状态:即dp[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。

则其状态转移方程便是:

dp[i][v]=max{dp[i-1][v],dp[i-1][v-cost[i]]+value[i]}
代码:

#include<iostream>
using namespace std;
int dp[1000][1000];
int max(int x,int y)
{
    return x>y?x:y;
}
int main()
{
    int t,n,v,i,j;
    int va[1000],vo[1000];
    cin>>t;
    while(t--)
    {
        cin>>n>>v;
        for(i=1;i<=n;i++)
            cin>>va[i];
        for(i=1;i<=n;i++)
            cin>>vo[i];
        memset(dp,0,sizeof(dp));//初始化操作
         for(i=1;i<=n;i++)
        {
            for(j=0;j<=v;j++)// 注意 j  是从 0 开始的  应为 背包的容量 可能为零 
            {
                if(vo[i]<=j)//表示第i个物品将放入大小为j的背包中
                    dp[i][j]=max(dp[i-1][j],dp[i-1][j-vo[i]]+va[i]);//第i个物品放入后,那么前i-1个物品可能会放入也可能因为剩余空间不够无法放入
                else //第i个物品无法放入
                    dp[i][j]=dp[i-1][j];
            }
        }
        cout<<dp[n][v]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值