Bone Collector II HDU - 2639(01背包 + 第k优解)

Bone Collector II

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 231).
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

题目大意:
t组输入,输入三个数n,m,k分别表示骨头的数量、包的体积和最优解k。然后输入n行表示骨头的价值,再输入n行表示骨头的体积。求体积为m的第k最大值。

思路:
01背包 + 第k优解
现在01的基础上多加一维,f[m][k],表示在m空间下第k大的价值。
更新的时候有两个数组aba记录选择第i个物品的从大到小排列,b记录不选择的从大到小排列,然后合并ab,选出ab里面前k个最大的。合并到f中。

完整代码:

#include<iostream>
#include<cstring>

using namespace std;
const int N = 1010, M = 35;

int f[N][M];   
int v[N],w[N];  // 骨头的体积,价值
int n,m,k;      // 骨头的数量;包的体积;最优解k
int a[N],b[N];  // 存储放和不放两种状态,a[]为放,b[]为不放

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(f,0,sizeof f);

        cin>>n>>m>>k;
        for(int i=1;i<=n;i++) cin>>w[i];
        for(int i=1;i<=n;i++) cin>>v[i];

        for(int i=1;i<=n;i++)
        {
            for(int j=m;j>=v[i];j--)
            {
                int x,y,z,s;
                for(s=1;s<=k;s++)               // 分别将放入第i个石头与不放第i个石头的结果存入a,b,数组之中
                {
                    a[s]=f[j-v[i]][s]+w[i];     // 放
                    b[s]=f[j][s];               // 不放
                }
                x=y=z=1;
                a[s]=b[s]=-1;
                while(z<=k && (x<=k || y<=k))   // 循环找出前K个的最优解
                {
                    if(a[x]>b[y])
                    {
                        f[j][z]=a[x];
                        x++;
                    }
                    else
                    {
                        f[j][z]=b[y];
                        y++;
                    }
                    if(f[j][z]!=f[j][z-1]) z++;
                }
            }
        }
        cout<<f[m][k]<<endl;;
    }
    return 0;
}

原题链接:
vjudge
hdu

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值