附题目链接: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. linux 压测jmeter24h稳定性测试

    环境准备: 安装jmeter,JDK: wget  https://archive.apache.org/dist/jmeter/binaries/apache-jmeter-5.1.tgz cd  ...

  2. Visual Studio Code修改全屏背景

    打开VSCode安装目录,找到workbench.desktop.main.css文件 在最后一行添加以下代码: body{ pointer-events:auto !important; backg ...

  3. MXnet的使用

    关于MXnet的介绍: MXNet: A flexible and efficient library for deep learning. 这是MXNet的官网介绍,“MXNet是灵活且高效的深度学 ...

  4. 【PAT甲级】1109 Group Photo (25分)(模拟)

    题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...

  5. C#中字符串常用方法

    string str = "123@163.com"; int index = str.IndexOf('@'); // 返回3  从左向右第一个@ int index = str ...

  6. logging basic

    logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等. 相比print,具备如下优点:        可以通过设置不同的日志等级, ...

  7. [PHP]新版的mongodb扩展安装和使用

    旧版的mongo扩展已经不推荐使用了,在php7以上一般是安装和使用新版的mongodb扩展 ubuntu下 apt-get install php-mongodb 例如下面的代码进行了查询和插入集合 ...

  8. Cesium 基于MapBox底图加载3DTiles 模型

    3DTiles 模型采用   CATIA V5 R22 --->3dxml --->GLB--->B3DM var extent = Cesium.Rectangle.fromDeg ...

  9. HGAME 2020 week1 web

    1.Cosmos 的博客 知识点:git source code leak 2.接 头 霸 王 Description HGAME Re:Dive 开服啦~ 打开题目,提示了"头" ...

  10. Java Interview Questions Summary

    Spring 高并发 Java基础 多线程 正文. 多线程精选53题 1.什么是线程 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器 ...