附题目链接: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. 如何在Access中使用SQL语句

    如何在Access中使用SQL语句 创建,查询设计,弹出[显示表]窗口,点击[关闭]将该窗口关掉.这时软件会进入[设计]工具栏,我们点击工具栏左侧的[SQL视图].[SQL视图]默认选择的是[设计视图 ...

  2. 解决fences2.01在win8.1的状态下无法移动桌面图标问题

    Fences 2.01破解版      链接:http://pan.baidu.com/s/1eSH2tGI 密码:o7oe Fences 2.01 win8.1修复补丁(stardock2.11)  ...

  3. 小白艰难的Python图像的绘制

    1.贪吃蛇 代码: import turtle turtle.setup(650,350) turtle.penup() turtle.fd(-250) turtle.pendown() turtle ...

  4. ubuntu建立软链接注意事项

    ln 参数 源文件 目标链接文件 -s:代表新建一个软链接,又称符号链接: eg.  ln -s /mnt/d/Documents/source.xlsx target.xlsx 1.目标文件的后缀名 ...

  5. 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)

    题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...

  6. 设备驱动基础学习--misc device简单实现

    在Linux驱动中把无法归类的五花八门的设备定义为混杂设备(用miscdevice结构体表述).miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号不同. 所有的miscde ...

  7. C语言中调用运行python程序

    C语言中调用运行python程序: Python代码如下: 创建test.py. #!/usr/bin/python3 #test.py import sys x = ]) print x*x 注意: ...

  8. 8.5-Day1T2--Asm.Def 的基本算法

    题目大意 给一棵树,求∑∑w_i*w_j*w_LCA(i,j) w_i表示i点权值 题解 显然一点点求lca是肯定会tle的 那就想如何优化 i和j的lca和j和i的lca是一样的 DFS,在每个x处 ...

  9. 6_15 给任务排序(UVa10305)<拓扑排序>

    John有n件事情要做,不幸的是这些事情并不是各自独立的,而是有相依性的.换句话说可能有某件事情一定要在另一件事情做完之后才能做.Input每组测试数据可能有好几列.第一列有2个整数n,m.(1 &l ...

  10. JavaScript 对象属性与方法

    对象的创建: 1 字面量创建 var obj = {a:1,b:2}; 2 构造函数创建 var obj = new Object(); obj.a = 1; obj.b = 2; 3 Object. ...