附题目链接: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. Mac下ssh远程无密码登录

    入手Mac,对很多工具的使用都不太熟悉,这不,做web开发,登录远程服务器非常繁琐,想要去掉输入密码这个环节,找到网友的分享如下: http://www.cnblogs.com/shuaiwhu/ar ...

  2. 利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试

    从一到二:利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试 通过从零到一的教程,我们已经得到了通过mnist训练集生成的caffemodel,主要包含下面四个文 ...

  3. js处理文章详情页点击量统计

    具体需求:在文章管理系统中,需要统计文章详情页面的点击量,来体现该文章是否受用户欢迎,方便管理员调整日后编辑新文章时的内容方向. 解决方案:设置有过期时间的cookie 我们在访问文章详情页面时,拿到 ...

  4. Spring - Spring 常用注解

    概述 简单整理一些 Spring 的注解 这个算是一个 水一波 类型的整理 内容不全 分类可能有的地方不会太符合逻辑 而且时间也不太充裕 先把自己想写的写下来, 然后随缘整理吧 约定 版本 Sprin ...

  5. 【PAT甲级】1108 Finding Average (20分)

    题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...

  6. logstash output到kafka记录与总结( No entry found for connection 2)

    简述 本文记录logstash的output配置为kafka的过程.这里是简单的例子,输入为stdin,本文主要目的是为了记录在这次配置过程中遇到的问题和解决的过程及总结. 关于kafka集群的搭建可 ...

  7. etc/hosts文件详解

    Linux 修改 etc/hosts文件 hosts文件 hosts —— the static table lookup for host name(主机名查询静态表). hosts文件是Linux ...

  8. base64加/解密算法C++实现

    base64编码原理:维基百科 - Base64 其实编码规则很简单,将字符串按每三个字符组成一组,因为每个字符的 ascii 码对应 0~127 之间(显然,不考虑其他字符集编码),即每个字符的二进 ...

  9. JenKins docker 集群

    //tag 桉树有时间来搞 **阿斯蒂 啊 阿斯蒂

  10. python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数。如果不存在这样一对整数,则输入一条消息进行说明。

    python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数.如果不存在这样一对整数,则输入一条消息 ...