附题目链接:Bone Collector II

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
最开始我想简单了,我以为只要把每一步记录下来然后排序就行,发现连样例都过不了.先把我的蒟蒻(错误的)思路粘一发,看看有多少老铁和我想的一样哈哈哈
#include<iostream>
#include<cstring>
#include<list>
#include<algorithm>
using namespace std;
int dp[][];
int w[];
int v[];
int sot[];
int n,maxa;
int x,maxv,k;
list<int> l;
int main()
{
cin>>n;
while(n--)
{
cin>>x>>maxv>>k;
for(int i=; i<=x; i++)
cin>>w[i];
for(int i=; i<=x; i++)
cin>>v[i];
for(int i=; i<=x; i++)
for(int j=; j<=maxv; j++)
{
if(j>=v[i])
dp[i][j]=max(dp[i][j],dp[i-][j-v[i]]+w[i]);
else
dp[i][j]=dp[i-][j];
l.push_back(dp[i][j]);
l.push_back(dp[i-][j-v[i]]+w[i]);
}
l.sort();
l.unique();
list<int>::iterator it;
int p=;
for(it=l.end(); it!=l.begin(); it--)
{
p++;
if(p==k+)
{
cout<<*it<<endl;
break;
}
}
memset(dp,,sizeof(dp));
l.clear();
}
return ;
}

好了,下面进入正解,平时写背包用dp[i]表示容量为i时的最优解,那么自然可以想到用dp[i][j]表示容量为i时的第j优解,

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int dp[][],d1[];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,k;
int w[],v[];
memset(dp,,sizeof(dp));
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; i++)
scanf("%d",&v[i]);
for(int i=; i<=n; i++)
scanf("%d",&w[i]);
for(int i=; i<=n; i++)
for(int j=m; j>=w[i]; j--)
{
int cnt=;
for(int q=; q<=k; q++)
{
d1[cnt++]=dp[j][q];
d1[cnt++]=dp[j-w[i]][q]+v[i];
}
sort(d1,d1+cnt);
int p=;
for(int q=cnt-; q>=; q--)
{
if(p>k)break;
if(q==cnt-||d1[q]!=d1[q+])
dp[j][p++]=d1[q];
}
}
printf("%d\n",dp[m][k]);
}
return ;
}

01背包第k最优解的更多相关文章

  1. HDU 2639 (01背包第k优解)

    /* 01背包第k优解问题 f[i][j][k] 前i个物品体积为j的第k优解 对于每次的ij状态 记下之前的两种状态 i-1 j-w[i] (选i) i-1 j (不选i) 分别k个 然后归并排序并 ...

  2. HDU 2639 Bone Collector II【01背包 + 第K大价值】

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  3. 杭电 2639 Bone Collector II【01背包第k优解】

    解题思路:对于01背包的状态转移方程式f[v]=max(f[v],f[v-c[i]+w[i]]);其实01背包记录了每一个装法的背包值,但是在01背包中我们通常求的是最优解, 即为取的是f[v],f[ ...

  4. Bone Collector II HDU - 2639 01背包第k最大值

    题意: 01背包,找出第k最优解 题解: 对于01背包最优解我们肯定都很熟悉 第k最优解的话也就是在dp方程上加一个维度来存它的第k最优解(dp[i][j]代表,体积为i能获得的第j最大价值) 对于每 ...

  5. HDU 3639 Bone Collector II(01背包第K优解)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. HDU2639(01背包第K大)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. (01背包 第k优解) Bone Collector II(hdu 2639)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639       Problem Description The title of this problem i ...

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

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. HDU2639Bone Collector II[01背包第k优值]

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. opencv:联通组件扫描

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  2. Java进阶学习(4)之继承与多态(上)

    继承 媒体资料库的设计 代码复制是质量不良的表现 不具有可扩展性和可维护性 继承 子类父类关系 子类继承了什么 先定义初始化,后构造器 子类和父类的关系 子类有变量和父类变量相同时,父类变量隐藏 父类 ...

  3. Stylus-import

    Stylus Import Disclaimer: In all places the @import is used with Stylus sheets, the @require could b ...

  4. VUE常用写法

    v-for: v-for ='item,key of data' v-for ='item,index in data'     @click='' @click='pop.show=false'   ...

  5. NABCD model作业

    1)N(Need需求) 随着人类生活的快速发展,给人们带来了许多的便利,同时也给我们带来了一些麻烦,而我的拼图这个小游戏可以在人们在无聊时玩一玩,也可以给小孩子开发智力. 2)A(Approach做法 ...

  6. maven的安装与使用(运行单元测试和打包等)

    maven的下载与安装 maven是用于java的自动化构建工具. 1.下载: http://maven.apache.org/download.cgi 下载maven包,比如 apache-mave ...

  7. 排序算法之归并排序的python实现

    采用分治法: 分割:递归地把当前序列平均分割成两半. 集成:在保持元素顺序的同时将上一步得到的子序列集成到一起(归并). 归并操作(归并算法),指的是将两个已经排序的序列合并成一个序列的操作.归并排序 ...

  8. 不需要图片,css+svg绘制动态loading加载图标

    1.html 部分: <div id="refershDiv" class="refershDiv"> <svg xmlns="ht ...

  9. 封装ajax库,post请求

    http状态码406是服务器无法根据客户端请求的内容特性完成请求 //整站功能方法库封装ajax请求,这里只针对post var methods = { //全站ajax请求状态处理 ajax: fu ...

  10. 吴裕雄 python 机器学习——模型选择损失函数模型

    from sklearn.metrics import zero_one_loss,log_loss def test_zero_one_loss(): y_true=[1,1,1,1,1,0,0,0 ...