题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:求01背包的第k优解 dp(i, j)表示容量为j时的i优解 对于第二维的操作和01背包几乎一样,只是我们只需要关注每一次取的前k大个即可. #include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <climits> #…
题目链接:https://vjudge.net/contest/103424#problem/A 题目大意: 第一行输入几组数据,第二行第一个数字代表物体个数,第二个数代表总体积.需要注意的是,第三排输入的是物品的价值,第四排的物品的体积.在不可以拆分物体的前提下,已知背包的总体积,最大能获取的价值是多少. 01背包模板题,没什么好说的.(附两种形式的dp数组解决方案) //一维dp数组实现(滚动数组) #include <iostream> #include <cstdio> #…
Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4178    Accepted Submission(s): 2174 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa…
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…
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 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5463    Accepted Submission(s): 2880 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par…
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3042 Accepted Submission(s): 1578 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in…
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…
Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1334    Accepted Submission(s): 666 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par…
其实这个问题,真的挺好想的,但是我咋想了那么久呢~~ 很好理解,第K大01背包一定基于01背包,dp数组也很容易的想到由dp[V]  ---->   dp[V][K],来表示背包容量是V时候的第K大背包 然后就是状态转移方程了,多写一写,你也能手推出来的,不能被吓到 dp[V][1] = max_第一大(dp[v][1],dp[v-w][1]+vi) dp[V][2] = max_第二大数(dp[v][1],dp[v-w][1]+vi,dp[v][2],dp[v-w][2]+vi) =  max…
这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004.blog.163.com/blog/static/8835120220138611342496/http://hi.baidu.com/chenyun00/item/1c6c44318acc8bfaa88428c7 #include <iostream> #include <cstdio&…
http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,k]可以理解为前i个物品,放入容量v的背包时,第K优解的值.时间复杂度为O(NVK). Talk is cheap. 看代码吧. import java.util.Scanner; public class BoneCollector { public static void main(String[] sur…
http://acm.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 does…
解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[v-c[i]]+w[i]中的最大值,但是现在要求第k大的值,我们就分别用两个数组保留f[v]的前k个值,f[v-c[i]]+w[i]的前k个值,再将这两个数组合并,取第k名. 即f的数组会增加一维. http://blog.csdn.net/lulipeng_cpp/article/details/…
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.c…
题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m,再以m为背包容量再进行一次01背包,dp[j]表示当物品的组合价值为j时,它们的体积之和的最小量.那么就求出了所有可能的价值,从1-m都有,但是其中一些是求不出来的,也就是骨头的价值不能组合成这个数字,那么就得过滤掉. #include <iostream> #include <cstdi…
题意: 01背包,找出第k最优解 题解: 对于01背包最优解我们肯定都很熟悉 第k最优解的话也就是在dp方程上加一个维度来存它的第k最优解(dp[i][j]代表,体积为i能获得的第j最大价值) 对于每一个物品只有两种选择情况 1.把这个物品加入背包 2.不要这个物品 那么它的前k种最优解也是由n种物品的这两个选择组成的 假设总体积是4,现在有两种物品,求第2最大值 1.体积1,价值3 2.体积1,价值2 最开始dp状态: 体积:   1  2  3  4 第1最大值:0  0  0  0 第2最…
题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j \right] = \max \left\{ {dp\left[ j \right],dp\left[ {j - a\left[ i \right].w} \right] + a\left[ i \right].v} \right\}\] 如果要求第K优解,那么状态 dp[j] 就应该是一个大小为…
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.c…
题目http://acm.hdu.edu.cn/showproblem.php?pid=2639 分析:这是求第K大的01背包问题,很经典.dp[j][k]为背包里面装j容量时候的第K大的价值. 从普通01背包中可以知道最大价值dp[j]是由dp[j]和dp[j-c[i]]+w[i]决定的.那么可以知道 dp[j][k]也是有dp[j][k]和dp[j-c[i]][k]来决定的. 求次优解.第K优解: 对于求次优解.第K优解类的问题,如果相应的最优解问题能写出状态转移方程.用动态规划解决,那么求…
题目链接 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 l…
题意:求01背包的第k最优值 输入:第一行为T,下面是T组数据,每组数据有n,m,k 代表n件物品,m容量,和题目要求的k,下一行是n个物品的价值,再一行是n个物品的体积 输出:T行答案 /* 类似于归并排序中合并的做法,对于f[i][j]的k个最优值,从f[i-1][j]和f[i-1][j-w[i]]+v[i] 的k个最优值中选出最优的k个放入 */ #include<cstdio> #include<iostream> #include<cstring> #defi…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意: 01背包第k优解, 背包九讲原题.“ 对于求次优解.第K优解类的问题,如果相应的最优解问题能写出状态转移方程.用动态规划解决,那么求次优解往往可以相同的复杂度解决,第K优解则比求最优解的复杂度上多一个系数K. 其基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并.这里仍然以01背包为例讲解一下. 首先看01背包求最优解的状态转移方程:f[i][v…
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:h…
求每个状态里的k优解,然后合并 /* HDU 2639 求01背包的第k大解. 合并两个有序序列 */ #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; ; ][];//dp[i][j]表示容量为i,第j大的值 int value[MAXN]; int weight[MAXN]; ]; ]; int N,V,K; v…
http://acm.hdu.edu.cn/showproblem.php?pid=2639 在背包的基础上维护一个size<=K的最大值集合,为什么维护K个就好了呢,因为如果当前状态有多余K个最优解,前K个就足够转移到下一状态并占满前K了,所以K个之后的都没必要维护. #include <iostream> #include <cstdio> #include <vector> #include <string> #include <map&g…
分析 \(dp[i][j][k]\)为枚举到前i个物品,容量为j的第k大解.则每一次状态转移都要对所有解进行排序选取前第k大的解.用两个数组\(vz1[],vz2[]\)分别记录所有的选择情况,并选择其中前k大的更新当前的dp[i][k].因为dp[i]满足递增的特点,所以可以对两个数组顺序比较选择. #include<bits/stdc++.h> using namespace std; const int maxn = 1e3+5; const int INF = 0x3f3f3f3f;…
此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就是2个数组合并之后排序,可是实际做法最好不要怎么做.由于你不知道总共同拥有多少种.而我们最多仅仅须要前K个大的即可了(由于可能2个数组加起来的组合数达不到K个),假设所有加起来数组开多大不清楚,所以能够选用归并排序中把左右2个有序数组合并成一个有序数组的方法来做.就是用2个变量去标记2个有序数组的头…
题目大意 一个人收藏骨头,有 n 个骨头,每个骨头有体积和价值,问能够装在容量为 V 的背包中,能获得的第 k 大(去重后)价值是多少. 样例 样例输入 1 5 10 2 1 2 3 4 5 5 4 3 2 1 样例输出 1 12 样例输入 2 5 10 12 1 2 3 4 5 5 4 3 2 1 样例输出 2 2 分析 跑暴力显然不优秀,每种物品可选可不选,最多 \(2^n\) 种不同的方案,也就对应这么多价值,显然如果都存下再排序输出结果,简单了事,但是显然时间和空间都承受不住. 当然在跑…
Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4229    Accepted Submission(s): 2205 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par…