【dp】Bone Collector II

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

题意: 01背包第k优解, 背包九讲原题。“

对于求次优解、第K优解类的问题,如果相应的最优解问题能写出状态转移方程、用动态规划解决,那么求次优解往往可以相同的复杂度解决,第K优解则比求最优解的复杂度上多一个系数K。

其基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并。这里仍然以01背包为例讲解一下。

首先看01背包求最优解的状态转移方程:
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}
。如果要求第K优解,那么状态f[i][v]就应该是一个大小为K的数组f[i][v][1..K]。其中f[i][v][k]表示前i个物品、背包大小为 v时,第k优解的值。“f[i][v]是一个大小为K的数组”这一句,熟悉C语言的同学可能比较好理解,或者也可以简单地理解为在原来的方程中加了一维。 显然f[i][v][1..K]这K个数是由大到小排列的,所以我们把它认为是一个有序队列。

然后原方程就可以解释为:f[i][v]这个有序队列是由f[i-1][v]和f[i-1][v-c[i]]+w[i]这两个有序队列合并得到的。有序队列f[i-1][v]即f[i-1][v][1..K],f[i-1][v-c[i]]+w[i]则理解为在f[i-1][v-c[i]] [1..K]的每个数上加上w[i]后得到的有序队列。合并这两个有序队列并将结果的前K项储存到f[i][v][1..K]中的复杂度是O(K)。最后的答案是f[N][V][K]。总的复杂度是O(VNK)。

” ---- 摘自背包九讲

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define Zero(x) memset(x, 0, sizeof(x))
using namespace std;
const int maxn = 1004;
int f[32][maxn];
int n, V, k;
int C[maxn];
int W[maxn];
int a[32], b[32];
int main() {
    int T;
    cin >> T;
    while (T--) {
        cin >> n >> V >> k;
        Zero(f);
        Zero(a);
        Zero(b);
        for (int i = 1; i <= n; ++i) {
            scanf("%d", W + i);
        }
        for (int i = 1; i <= n; ++i) {
            scanf("%d", C + i);
        }
        for (int i = 1; i <= n; ++i) {
            for (int v = V; v >= C[i]; v--) {
                for (int j = 1; j <= k; ++j) {
                    a[j] = f[j][v];
                    b[j] = f[j][v - C[i]] + W[i];
                }
                int x, z, y;
                x = y = z = 1;
                while (z <= k && (x <= k || y <= k)) {
                    if (a[x] >= b[y]) {
                        f[z][v] = a[x];
                        x++;
                    } else {
                        f[z][v] = b[y];
                        y++;
                    }
                    if (f[z - 1][v] != f[z][v]) z++;
                }
            }
        }
        printf("%d\n", f[k][V]);
    }
}
时间: 2024-11-03 22:15:12

【dp】Bone Collector II的相关文章

【HDU2639】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): 2948    Accepted Submission(s): 1526 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa

【HDU2602】Bone Collector(01背包)

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

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

Bone Collector II(HDU 2639 DP)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3471    Accepted Submission(s): 1792 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 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个有序数组的头,然后比

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【LeetCode】Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

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