【01背包-Kth优解】HDU2639-Bone Collector II

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2639

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

第一次做这种类型的题目,有必要说明一下。

普通的01背包,求得都是最优解。但是我们如果深入的理解了01背包,你应该很清楚,01背包每次最优解和次优解里面选出最优解(因为每一个数只有取或者不取)。

所以,我们要明白,其实我们之前所写过的01背包题,解的所有情况我们都有计算,只是没有保存,选择性的保存了最优解。而现在,我们需要Kth优解,那么我们就要保

存起来。所以我们增加一个维度,来记录第k优解。

Kth优解01背包代码:(排序,效率低)

Accepted2639780MS1716K1269 BG++wlxsq

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxv=1005;
const int maxk=35;
const int maxn=105;
int w[maxn],v[maxn];
int dp[maxv][maxk];     //  dp[i][j]表示容量为i的第j优解;
int N,V,K;
int a[maxn];
bool cmp(int a,int b)
{
    return a>b;
}
void init()
{
    memset(dp,0,sizeof(dp));
}
void Kth_ZeroOnePack()
{
    for(int i=1;i<=N;i++){      //  物品枚举;
        for(int j=V;j>=w[i];j--){
            int cnt=0;
            for(int k=0;k<K;k++){
                a[cnt++]=dp[j][k];
                a[cnt++]=(dp[j-w[i]][k]+v[i]);
            }
            sort(a,a+cnt,cmp);
            cnt=1;
            dp[j][0]=a[0];      //  最优解;
            for(int k=1;k<K*2&&cnt<K;k++){  //  将去重之后的有大到小赋值给dp;
                if(a[k]!=a[k-1]){
                    dp[j][cnt++]=a[k];
                }
            }

        }
    }
    printf("%d\n",dp[V][K-1]);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d%d%d",&N,&V,&K);
        for(int i=1;i<=N;i++)
            scanf("%d",&v[i]);
        for(int i=1;i<=N;i++)
            scanf("%d",&w[i]);
        Kth_ZeroOnePack();
    }
    return 0;
}


不排序代码:
Accepted2639124MS1708K2686 BG++wlxsq

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxv=1005;
const int maxk=35;
const int maxn=105;
int w[maxn],v[maxn];
int dp[maxv][maxk];     //  dp[i][j]表示容量为i的第j优解;
int N,V,K;
int a[maxn],b[maxn];
bool cmp(int a,int b)
{
    return a>b;
}
void init()
{
    memset(dp,0,sizeof(dp));
}
void Kth_ZeroOnePack()
{
    for(int i=1;i<=N;i++){      //  物品枚举;
        for(int j=V;j>=w[i];j--){
            int cnt=0;
            for(int k=0;k<K;k++){
                a[k]=dp[j][k];
                b[k]=(dp[j-w[i]][k]+v[i]);
            }
            a[K]=-1,b[K]=-1;    //  这里目的是为了让a,b两个数组都比较完;
            int x=0,y=0,z=0;
            while(z<K&&(a[x]!=-1||b[y]!=-1)){   //  只有两个同时比较完,或者已经大于我们要找的K了while才会结束
                if(a[x]>b[y])
                    dp[j][z]=a[x++];
                else
                    dp[j][z]=b[y++];
                if(dp[j][z]!=dp[j][z-1])
                    z++;
            }
        }
    }
    printf("%d\n",dp[V][K-1]);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d%d%d",&N,&V,&K);
        for(int i=1;i<=N;i++)
            scanf("%d",&v[i]);
        for(int i=1;i<=N;i++)
            scanf("%d",&w[i]);
        Kth_ZeroOnePack();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值