附题目链接: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. Ubuntu18.04安装caffe python3.6 opencv3.2 CPU

    设置ubuntu的softwares&updates的源为国内源,这样会提高下载速度. 如果是安装python相关库,为提高速度使用: pip3 install 要下载的库 -i https: ...

  2. 问题 A: 【贪心】排队接水

    问题 A: [贪心]排队接水 时间限制: 1 Sec  内存限制: 128 MB[命题人:外部导入] 题目描述 有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种 ...

  3. IDE - IDEA - tab - 方法相关的移动

    1. 概述 标题可能会改 一个 tab 里方法相关的操作 2. 前提 以默认的模式编辑 tab 对我来说, 就关掉 vim 插件 3. 操作 1. 查看文件结构 概述 唤出当前文件的 结构 唤出后可以 ...

  4. MyBatis-Spring整合之方式3

    通过注解整合 1.在UserDao同级目录下新建接口UserMapper,内容如下: public interface UserMapper { @Select("select * from ...

  5. 关于AD元件的命名

    1.电容 C? 10uf 2.电阻 R? 10k 3.芯片 U? STM32F103VET6 4.单排 J?   SIP 5.三极管 Q? s8550 6.晶振 Y? 12M

  6. html学习-第二集(CSS)

    head标签里面添加style标签,可以为标签添加样式 id选择器 类选择器 标签选择器 层级选择器 组合选择器 属性选择器 <!DOCTYPE html> <html lang=& ...

  7. vs2013编译soui并创建一个项目

    1.soui.10.sln改为soui.13.sln 2.用nodepad++打开soui.13.sln,作如下修改 注:第一行我是改为13.00,编译后似乎自己变为12.00了 编译结果: 注:这是 ...

  8. HashMap遍历,取出key和value

    HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,在用keySet(key)取value时候,需要key 第一种: Map map = new HashMap( ...

  9. 【C语言】创建一个函数,利用该函数将字符串中的小写字母转换为大写字母

    原理: 这类题目主要通过ASCII(美国信息交换标准代码)码差值实现,A对应ASCII码十进制数字是65,a对应ASCII码十进制数字是97,即大小写字母之间ASCII码差值为32,想要将大写字母转换 ...

  10. PHP高级面试题

    在网上看到一些高级php 的面试题目.. 最近接连面试了几家公司,有些重要问题记录一下,督促自己学习提高,同时希望给朋友们一些帮助.内容很多,一点点完善,一步步学习..有些是面试被问,有些是招聘要求, ...