背包问题——无限物品的背包问题,0-1背包问题与劲歌金曲(uva12563)

无限物品的背包问题

物品无限的背包问题。有n种物品,每种均有无穷多个。第i种物品的体积为Vi,重量为Wi。选一些物品装到一个容量为C的背包中,使得背包内物品在总体积不超过C的前提下重量尽量大。1≤n≤1001≤n≤100,1≤Vi≤C≤10000,1≤Wi≤106。

测试数据:
3 5
1 2
2 3
3 2
answer:10

3 7
2 1
3 2
4 3
answer:5

3 5
3 3
4 2
3 2
answer:3

0

解题思路

dp思维:
每个物品都可以无限使用,且我们不需要考虑能否完全利用背包的容量C,只要所装物品的重量总和最大即可。由于物品是无限使用的,所以在状态转移中我们所需要思考的是此时背包的容量,而不是对于面向物品思考状态。
状态转移方程:
i代表着背包的容积
dp【i】=max(dp【i】,dp【i-v【j】】+w【j】)

递归思维:
相当于一个有向图的动态规划,我们对每个重量都探讨它接下可装物品的最大重量。

递归:

#include 
#include 
#include 
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))
//由于物品无限且不需要如硬币问题一样必须装的恰到好处
int d[M],C;
int v[N],w[N];
int dp(int C,int n)
{
     
   int *ans=&d[C],i;   //ans改变d【c】改变
   if(*ans>0) return *ans;
   *ans=0;
   for(i=0;i<n;i++)
    if(C>=v[i])
      *ans=Max(*ans,dp(C-v[i],n)+w[i]);
    return *ans;
}

int main()
{
     
   int n,ans,i;
   while(~scanf("%d%d",&n,&C))
   {
     
       for(i=0;i<n;i++)
        scanf("%d%d",&v[i],&w[i]);
       memset(d,0,sizeof(int)*M);   //初始化
       ans=dp(C,n);
       printf("ans:%d\n",ans);
   }
   return 0;
}

递推:

#include 
#include 
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))

int main()
{
     
    int C,n;
    while(~scanf("%d%d",&n,&C))
    {
     
        int i,j;
        int v[n],w[n];
        int dp[M]={
     0};  //初始化为0

        for(i=0;i<n;i++)
            scanf("%d%d",&v[i],&w[i]);

        for(i=1;i<=C;i++)
            for(j=0;j<n;j++)
            if(i>=v[j])
                dp[i]=Max(dp[i],dp[i-v[j]]+w[j]);
        printf("ans:%d\n",dp[C]);
    }
    return 0;
}

01背包问题

有n种物品,每种均只有1个。第i种物品的体积为Vi,重量为Wi。选一些物品装到一个容量为CC的背包中,使得背包内物品在总体积不超过C的前提下重量尽量大。1≤n≤100,1≤Vi≤C≤10000,1≤Wi≤106。

测试数据:
5 6             
1 2
2 4
3 4
4 5 
5 6
answer:10

解题思路

递归

由于每个物品只能使用一次,所以我们需要标记,类似于DFS进行标记与恢复标记,再利用图的思维,对着不同体积,探讨它接下来体积所能装下物品重量的最大值

#include 
#include 
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))

int flag[N];   //物品只能用一次需要标记类似dfs
int n,v[N],w[N];
int dp(int C)   //为什么不加入记忆化?由于标记的存在并不适合记忆化.
{
     
   int ans=0,i;
   for(i=0;i<n;i++)
     if(!flag[i] && C>=v[i])
     {
     
         flag[i]=1;
         ans=Max(ans,dp(C-v[i])+w[i]);
         flag[i]=0; //恢复状态
     }
   return ans;
}

int main()
{
     
    int C;
    while(~scanf("%d%d",&n,&C))
    {
     
        int i;
        for(i=0;i<n;i++)
            scanf("%d%d",&v[i],&w[i]);
        printf("ans:%d\n",dp(C));
    }
    return 0;
}

递推

二维打表思维*
这回是由于物品只能思考一个,所以我们所要面对思考的对象是物品,而不是此时背包的容量。为什么不面向背包的容量进行思考呢?当你面向背包进行思考时,你就需要加入一个标记,而既然时递推,你就无法像上面那样进行多次尝试,也就是说是递推限制了我们面向背包容量的思考。
我们要对每个物品需要考虑:装或者不装。那么我们的状态转移方程就将由此产生,装下去大呢还是不装下去大呢
总结:这是把前i个物品装入背包中的最大重量和的状态转移
状态转移方程:
dp【i】【j】=max(dp【i-1】【j】,dp【i-1】【j-v【i】】+w【i】) (i代表物品数,j代表重量)

这里我们用的是自底向上的思维

#include 
#include 
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))

int dp[N][M];
int main()
{
     
    int C,n,i,j,v[N],w[N];
    while(~scanf("%d%d",&n,&C))
    {
     
        for(i=1;i<=n;i++)      //由于要预留dp【0】的位置
            scanf("%d%d",&v[i],&w[i]);

        for(i=0;i<N;i++)
            for(j=0;j<M;j++)
            dp[i][j]=0;

        for(i=1;i<=n;i++)
            for(j=1;j<=C;j++)
         if(j>=v[i])
            dp[i][j]=Max(dp[i-1][j],dp[i-1][j-v[i]]+w[i]);
          else
            dp[i][j]=dp[i-1][j];
        printf("%d\n",dp[n][C]);
    }
    return 0;
}

我们发现每次dp只需要用到上一层的数组那么我们可以对dp数组进行优化,变成一维。
注意:这里对容量C我们需要自顶向下,不然会出现dp改变了前面的数,dp【j-v【i】】+w【i】中的dp【j-v【i】】用的是改变后的数,不是我们所需要的保存上一层数组的数。

#include 
#include 
#define N 103
#define M 100003
#define Max(a,b) ((a)>(b)?(a):(b))


int main()
{
     
    int n,C,i,j;
    while(~scanf("%d%d",&n,&C))
    {
     
        int dp[M]={
     0};
        int v[N],w[N];
        for(i=0;i<n;i++)
            scanf("%d%d",&v[i],&w[i]);
        for(i=0;i<n;i++)
            for(j=C;j>=v[i];j--)   //这里要自顶向下不然会出现dp【i】改变了前面的数导致后面的j-v【i】用的是改变后的数导致结果异常
                dp[j]=Max(dp[j],dp[j-v[i]]+w[i]);
        printf("ans%d\n",dp[C]);
    }
    return 0;
}

还可再进行一点简单的优化:

 for(i=0;i<n;i++){
     
     scanf("%d%d",&v,&w);
     for(j=C;j>=v;j--)  
             dp[j]=Max(dp[j],dp[j-v]+w);
  }

劲歌金曲

(If you smiled when you see the title, this problem is for you _)
For those who don’t know KTV, see: https://en.wikipedia.org/wiki/Karaoke_box
There is one very popular song called Jin Ge Jin Qu(劲歌金曲). It is a mix of 37 songs, and is
extremely long (11 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, and
some other unofficial versions. But in this problem please forget about them.
Why is it popular? Suppose you have only 15 seconds left (until your time is up), then you should
select another song as soon as possible, because the KTV will not crudely stop a song before it ends
(people will get frustrated if it does so!). If you select a 2-minute song, you actually get 105 extra
seconds! ….and if you select Jin Ge Jin Qu, you’ll get 663 extra seconds!!!
Now that you still have some time, but you’d like to make a plan now. You should stick to the
following rules:
? Don’t sing a song more than once (including Jin Ge Jin Qu).
? For each song of length t, either sing it for exactly t seconds, or don’t sing it at all.
? When a song is finished, always immediately start a new song.
Your goal is simple: sing as many songs as possible, and leave KTV as late as possible (since we
have rule 3, this also maximizes the total lengths of all songs we sing) when there are ties.
Input
The first line contains the number of test cases T (T ≤ 100). Each test case begins with two positive
integers n, t (1 ≤ n ≤ 50, 1 ≤ t ≤ 109
), the number of candidate songs (BESIDES Jin Ge Jin Qu)
and the time left (in seconds). The next line contains n positive integers, the lengths of each song, in
seconds. Each length will be less than 3 minutes — I know that most songs are longer than 3 minutes.
But don’t forget that we could manually “cut” the song after we feel satisfied, before the song ends. So
here “length” actually means “length of the part that we want to sing”.
It is guaranteed that the sum of lengths of all songs (including Jin Ge Jin Qu) will be strictly larger
than t.
Output
For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengths
of songs that you’ll sing.
Explanation:
In the first example, the best we can do is to sing the third song (80 seconds), then Jin Ge Jin Qu
for another 678 seconds.
In the second example, we sing the first two (30+69=99 seconds). Then we still have one second
left, so we can sing Jin Ge Jin Qu for extra 678 seconds. However, if we sing the first and third song
instead (30+70=100 seconds), the time is already up (since we only have 100 seconds in total), so we
can’t sing Jin Ge Jin Qu anymore!Universidad de Valladolid OJ: 12563 – Jin Ge Jin Qu hao 2/2
Sample Input
2
3 100
60 70 80
3 100
30 69 70
Sample Output
Case 1: 2 758
Case 2: 3 777

题意:
假如你的ktv还有15秒就时间到了,但是当你正在唱一首歌的时候他们并不会把你赶走而是会等该歌曲播完才让你离开,这时你可以点一首两分钟的歌,相当于你多唱了105秒。
假如你正在ktv唱歌,还剩下t秒时间,你决定接下来只唱你最爱的n首歌,在时间结束之前你会再点一首《劲歌金曲》(时长678秒),求出你一共唱了几首歌和唱了多长时间。
输入n(n<=50),t(t<=10^9)和每首歌的长度(保证不超过三分钟)。

解题思路
单纯的思考所唱的时间最长,就是一个简单的01背包问题,边界有所不同是t-1(留一秒给劲歌金曲)。

#include 
#include 
#define Max(a,b) ((a)>(b)?(a):(b))
int main()
{
     
    int T,i,j;
    scanf("%d",&T);
    while(T--)
    {
     
        int n,t,time[50];
        int *dp;
        scanf("%d%d",&n,&t);
        dp=(int *)calloc(t,sizeof(int));   //初始化为0
        for(i=0;i<n;i++)
            scanf("%d",&time[i]);
        for(i=0;i<n;i++)
         for(j=t-1;j>=time[i];j--)
          dp[j]=Max(dp[j],dp[j-time[i]]+time[i]);
        printf("%d\n",dp[t-1]+678);
    }
    return 0;
}

那么我们再加入一个歌曲数的观念,你会发现每次歌曲数增加都伴随着dp(歌唱时间)的增加,那么我们只要将歌曲数的状态方程放入循环中同dp一起增加即可。

#include 
#include 
#define Max(a,b) ((a)>(b)?(a):(b))
int main()
{
     
    int T,i,j;
    scanf("%d",&T);
    while(T--)
    {
     
        int n,t,time[50];
        int *dp,*count;
        scanf("%d%d",&n,&t);
        dp=(int *)calloc(t,sizeof(int));   //初始化为0
        count=(int *)calloc(t,sizeof(int));
        for(i=0;i<n;i++)
            scanf("%d",&time[i]);
        for(i=0;i<n;i++)
         for(j=t-1;j>=time[i];j--)
          {
     
              count[j]=Max(count[j],count[j-time[i]]+1);
              dp[j]=Max(dp[j],dp[j-time[i]]+time[i]);
          }
         printf("%d %d\n",count[t-1]+1,dp[t-1]+678);
    }
    return 0;
}

你可能感兴趣的:(背包问题——无限物品的背包问题,0-1背包问题与劲歌金曲(uva12563))