0-1背包 第 K大 hdu2639

Bone Collector II

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


 

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 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
 

    思想与01背包相同,不过在动态规划过程中,不可以把前一个状态的最优解扔掉,而要保存下来。

在01背包中,状态数组是 f [ v ] ,表示容积为v时的最优决策。而现在,我不仅要知道最优决策,我还想知道稍微差一点的决策,即第2决策、第3决策....排个名。f [ v ] 我们可以看做是 f [ v ] [ 1 ] 这样的二维数组,他的第二维只有一个元素,也就是最优决策。现在我们增大第二维,比如 f [ v ] [ 3 ] ,意思是,不仅保留了最优解,次解也保留下来了。

也可以理解为,第二维是一个集合,集合里就存了所有可能的决策,并按大小有序,排在第一的就是最优解。

现在问题是 第k个最优决策是多少。

           在01背包里,我们只保留了最优解,而把不是最优解的解直接舍弃了

code:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;

const int N = 105;
int dp[1005][33];  // 加了一维表示 第 k 大  即 dp[nax_v][1] 表示 背包容量为 max_v 时候 取得 第一大的值
int cost[N];
int val[N];
int a[33],b[33];

int main()
    {
        int t,n,max_v,k;
        scanf("%d",&t);
        while(t--){
            memset(dp,0,sizeof(dp));
            memset(cost,0,sizeof(cost));
            memset(val,0,sizeof(val));
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            scanf("%d%d%d",&n,&max_v,&k);
            for(int i = 1; i <= n; ++i){
                scanf("%d",&val[i]);
            }
            for(int i =1; i <= n; ++i){
                scanf("%d",&cost[i]);
            }
            int d;
            for(int i = 1; i <= n; ++i){
                for(int j = max_v; j >= cost[i]; --j){
                    for(d = 1; d <= k; ++d){
                        a[d] = dp[j][d]; // 未取
                        b[d] = dp[j-cost[i]][d] + val[i]; //取了当前的
                    }
                    a[d] = -1;  // 保证下面 while循环 正常终止
                    b[d] = -1;
                    int p = 1,q = 1,r = 1;
                    while(p <= k &&(q <= k || r <= k)){
                        if(a[q] < b[r])
                            dp[j][p] = b[r++];
                        else
                            dp[j][p] = a[q++];
                        if(dp[j][p] != dp[j][p-1]) // 值相等 时候 过滤掉
                            ++p;
                    }
                }
            }
            printf("%d\n",dp[max_v][k]);
        }


        return 0;
    }

参考:https://blog.csdn.net/winter2121/article/details/70185099

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值