HDU 2639 Bone Collector II(01背包变型)

此题就是在01背包问题的基础上求所能获得的第K大的价值。

具体做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,其实就是2个数组合并之后排序,但是实际做法最好不要怎么做,因为你不知道总共有多少种,而我们最多只需要前K个大的就行了(因为可能2个数组加起来的组合数达不到K个),如果全部加起来数组开多大不清楚,所以可以选用归并排序中把左右2个有序数组合并成一个有序数组的方法来做,就是用2个变量去标记2个有序数组的头,然后比较,选了这个就这个变量加加,唯一不同的是不能有重复的,那么可以用一个很巧妙的办法,就是在循环结束前判断要加进去的数是否跟前一个一样,如果不一样才加加。

AC代码:

#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<ctime>
using namespace std;
#define push_back pb
int w[105],vol[105],vis[1005];
int dp[1005][35];
int main()
{
//    freopen("input.txt","r",stdin);
//    freopen("o1.txt","w",stdout);

    int i,j,k,t,n,v,K;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d%d%d",&n,&v,&K);
        for(i = 0; i < n; i++) scanf("%d",&w[i]);
        for(i = 0; i < n; i++)
            scanf("%d",&vol[i]);
        for(i = 0; i <= v; i++) vis[i] = 1;
        int temp[40];
        for(i = 0; i < n; i++)
        {
            for(j = v; j >= vol[i]; j--)
            {
                memset(temp,0,sizeof(temp));
                int a = 1,b = 1;
                k = 1;
                while((a<=vis[j-vol[i]] || b<=vis[j]) && k <= K)
                {
                    if((dp[j-vol[i]][a]+w[i] > dp[j][b] && a <= vis[j-vol[i]]) || b > vis[j])
                    {
                        temp[k] = dp[j-vol[i]][a]+w[i];
                        a++;
                    }
                    else
                    {
                        temp[k] = dp[j][b];
                        b++;
                    }
                    if(temp[k] != temp[k-1]) k++;
                }
                vis[j] = k;
                for(k = 1; k <= vis[j]; k++) dp[j][k] = temp[k];
            }
        }
        printf("%d\n",dp[v][K]);
    }
    return 0;
}

HDU 2639 Bone Collector II(01背包变型),布布扣,bubuko.com

时间: 2024-10-08 09:30:28

HDU 2639 Bone Collector II(01背包变型)的相关文章

HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4739    Accepted Submission(s): 2470 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

hdu 2639 Bone Collector II 01背包问题 求第K大最优值。。

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2665    Accepted Submission(s): 1392 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

hdu 2639 Bone Collector II(01背包 第K大价值)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3355    Accepted Submission(s): 1726 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par

HDU 2639 Bone Collector II

Bone Collector II Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 263964-bit integer IO format: %I64d      Java class name: Main The title of this problem is familiar,isn't it?yeah,if you had took part in the

hdu 2602 Bone Collector 【01背包模板】

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 40404    Accepted Submission(s): 16786 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bo

hdu 2602 Bone Collector(01背包)

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 42179    Accepted Submission(s): 17543 Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bon

hdu 2602 Bone Collector(01背包)模板

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 54132    Accepted Submission(s): 22670 Problem Description Many years ago , in

hdu 2602 - Bone Collector(01背包)解题报告

Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 39532    Accepted Submission(s): 16385 Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bo

HDU - 2602 Bone Collector(01背包讲解)

题意:01背包:有N件物品和一个容量为V的背包.每种物品均只有一件.第i件物品的费用是volume[i],价值是value[i],求解将哪些物品装入背包可使价值总和最大. 分析: 1.构造二维数组:dp[i][j]---前i件物品放入一个容量为j的背包可以获得的最大价值. dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - volume[i]] + value[i]);---(a) (1)dp[i - 1][j]---不放第i件物品,因此前i件物品放入一个容量为