HDU 2639 Bone Collector II (求第K大的背包)

点击打开链接

Bone Collector II

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2089    Accepted Submission(s): 1097


Problem Description
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must have seem this title.If you haven't seen it before,it doesn't matter,I will give you a link:

Here is the link: http://acm.hdu.edu.cn/showproblem.php?pid=2602

Today we are not desiring the maximum value of bones,but the K-th maximum value of the bones.NOTICE that,we considerate two ways that get the same value of bones are the same.That means,it will be a strictly decreasing sequence from the 1st maximum , 2nd maximum .. to the K-th maximum.

If the total number of different values is less than K,just ouput 0.
 

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, K(N <= 100 , V <= 1000 , K <= 30)representing the number of bones and the volume of his bag and the K we need. 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 K-th maximum of the total value (this number will be less than 2 31).
 

Sample Input
   
   
   
   
3 5 10 2 1 2 3 4 5 5 4 3 2 1 5 10 12 1 2 3 4 5 5 4 3 2 1 5 10 16 1 2 3 4 5 5 4 3 2 1

Sample Output
   
   
   
   
12 2 0
题意:求背包问题的第K大值。

普通01背包的状态转移方程为:dp[i][j]=max(dp[i-1][j],dp[i-1][j-v]+w)。由于状态转移的过程中已经枚举了所有策略,所以假设我们已经知道前面的状态的前K优解,那么当前状态的前k优解也可以求得。我们用dp[i][j][k]表示前i个物品,花费为j的第K优解,我们把dp[i][j]看成前i个物品花费为j的解的序列,那么dp[i][j]序列就是由dp[i-1][j]序列与dp[i-1][j-v]+w序列合并而成的新序列,由于我们要求K优解,所以我们只需要保留新序列的前K优值就行了。

背包九讲上说:对于求次优解、第K优解类的问题,如果相应的最优解问题能写出状态转移方程、用动态规划解决,那么求次优解往往可以相同的复杂度解决,第K优解则比求最优解的复杂度上多一个系数K。其基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并。

具体细节见代码:

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#define nn 1100
#define inff 0x3fffffff
#define mod 1000000007
using namespace std;
typedef long long LL;
int n,v,k;
int val[nn],w[nn];
int dp[110][1100][35];
int f[nn];
int main()
{
    int t,i,j,g;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&v,&k);
        for(i=1;i<=n;i++)
            scanf("%d",&val[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(i=0;i<=n;i++)
        {
            for(j=0;j<=v;j++)
            {
                for(g=1;g<=k+1;g++)
                    dp[i][j][g]=-inff;
            }
        }
        for(i=0;i<=v;i++)
            dp[0][i][1]=0;
        int ix1,ix2;
        for(i=1;i<=n;i++)
        {
            for(j=v;j>=0;j--)
            {
                for(g=1;g<=k;g++)
                {
                    dp[i][j][g]=dp[i-1][j][g];
                    f[g]=dp[i][j][g];
                }
                ix1=ix2=1;
                if(j-w[i]<0)
                    continue;
                for(g=1;g<=k;g++)
                {
                    if(f[ix1]>dp[i-1][j-w[i]][ix2]+val[i])
                    {
                        dp[i][j][g]=f[ix1];
                        ix1++;
                    }
                    else if(f[ix1]<dp[i-1][j-w[i]][ix2]+val[i])
                    {
                        dp[i][j][g]=dp[i-1][j-w[i]][ix2]+val[i];
                        ix2++;
                    }
                    else
                    {
                        dp[i][j][g]=f[ix1];
                        ix1++;
                        ix2++;
                    }
                }
            }
        }
        printf("%d\n",max(0,dp[n][v][k]));
    }
    return 0;
}

其实空间可以优化到二维,代码如下:

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#define nn 1100
#define inff 0x3fffffff
#define mod 1000000007
using namespace std;
typedef long long LL;
int n,v,k;
int val[nn],w[nn];
int dp[1100][35];
int f[nn];
int main()
{
    int t,i,j,g;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&v,&k);
        for(i=1;i<=n;i++)
            scanf("%d",&val[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(i=0;i<=n;i++)
        {
            for(j=0;j<=v;j++)
            {
                for(g=1;g<=k+1;g++)
                    dp[j][g]=-inff;
            }
        }
        for(i=0;i<=v;i++)
            dp[i][1]=0;
        int ix1,ix2;
        for(i=1;i<=n;i++)
        {
            for(j=v;j>=0;j--)
            {
                for(g=1;g<=k;g++)
                {
                    f[g]=dp[j][g];
                }
                ix1=ix2=1;
                if(j-w[i]<0)
                    continue;
                for(g=1;g<=k;g++)
                {
                    if(f[ix1]>dp[j-w[i]][ix2]+val[i])
                    {
                        dp[j][g]=f[ix1];
                        ix1++;
                    }
                    else if(f[ix1]<dp[j-w[i]][ix2]+val[i])
                    {
                        dp[j][g]=dp[j-w[i]][ix2]+val[i];
                        ix2++;
                    }
                    else
                    {
                        dp[j][g]=f[ix1];
                        ix1++;
                        ix2++;
                    }
                }
            }
        }
        printf("%d\n",max(0,dp[v][k]));
    }
    return 0;
}


你可能感兴趣的:(dp,动态规划,ACM,背包)