Bone Collector II

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4739    Accepted Submission(s): 2470

Problem Description
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must have seem this title.If you haven't seen it before,it doesn't matter,I will give you a link:
Here is the link:http://acm.hdu.edu.cn/showproblem.php?pid=2602
Today we are not desiring the maximum value of bones,but the K-th maximum value of the bones.NOTICE that,we considerate two ways that get the same value of bones are the same.That means,it will be a strictly decreasing sequence from the 1st maximum , 2nd maximum .. to the K-th maximum.
If the total number of different values is less than K,just ouput 0.
 
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
 
Author
teddy
 
Source
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639

题目大意:

见之前的收集骨头的博客,题意类似,给定背包容量,骨头的个数和每个骨头的价值,这次不是求在背包容量允许的情况下,最多装的价值,而是求在背包容量内,可以装的第k大价值,如果没有第k个最大值,那么输出0

输入包括多组样例,第一行输入一个T,样例的个数,接下来每个样例都有三行,第一行包括三个整数,N,V,K,分别代表骨头的个数,背包的容量,我们需要输出的第K个最大值,第二行包括N个数,分别代表骨头的数量和接下来一行有N个数,分别表示每种骨头的价值。

输出第K个最大价值,每个样例输出一行

思路:简单的01背包基础上做,要求的是第K个最大值,那么不用dp[j]=max(dp[j],dp[j-w[i]]+v[i])的状态转移方程,而是将两个值都记录下来,用for循环走一遍,记录下,容量为1到M的各个最大价值,dp[i][j]表示当背包容量为i时的第j个最大价值,最后只需要输出dp[m][k]即可!

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
int w[];
int v[];
int dp[][];
int d1[];
int d2[];
int main()
{
int t,n,m,k,x,y,z,p;
scanf("%d",&t);
while(t--)
{
memset(w,,sizeof(w));
memset(v,,sizeof(v));
memset(dp,,sizeof(dp));
memset(d1,,sizeof(d1));
memset(d2,,sizeof(d2));
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++)//01背包变形
{
for(int j=m;j>=w[i];j--)
{
for(p=;p<=k;p++)
{
d1[p]=dp[j][p];
d2[p]=dp[j-w[i]][p]+v[i];
}
d1[p]=d2[p]=-;
x=y=z=;
while((d1[x]!=-||d2[y]!=-)&&z<=k)
{
if(d1[x]>d2[y])
{
dp[j][z]=d1[x];
x++;
}
else
{
dp[j][z]=d2[y];
y++;
}
if(dp[j][z-]!=dp[j][z])
z++;
}
}
}
printf("%d\n",dp[m][k]);
}
return ;
}

HDU 2639 Bone Collector II(01背包变形【第K大最优解】)的更多相关文章

  1. HDU 2639 Bone Collector II (01背包,第k解)

    题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m ...

  2. hdu–2369 Bone Collector II(01背包变形题)

    题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...

  3. HDU 2639 Bone Collector II(01背包变型)

    此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...

  4. HDU - 2639 Bone Collector II (01背包第k大解)

    分析 \(dp[i][j][k]\)为枚举到前i个物品,容量为j的第k大解.则每一次状态转移都要对所有解进行排序选取前第k大的解.用两个数组\(vz1[],vz2[]\)分别记录所有的选择情况,并选择 ...

  5. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

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

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

  7. 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 ...

  8. hdu 2639 Bone Collector II

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

  9. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

随机推荐

  1. socket编程部分API

    以TCP协议为例,UNIX的相关API int socket(int domain , int type , int protocol); 根据指定的地址族,数据类型,协议来分配一个socket的描述 ...

  2. python变量字符拼接

    cpu = instances['vcpus_current'] cpu1 = str(cpu) + '核' memory = instances['memory_current'] / 1024 m ...

  3. C#基础在using中创建对象

    在using中创建的对象的类必须是实现了IDispose接口的类,示例代码如下: static void Main(string[] args) { Method(); Console.WriteLi ...

  4. 如何用VSCode愉快的写Python

    在学习Python的过程中,一直没有找到比较趁手的第三方编辑器,用的最多的还是Python自带的编辑器.由于本人用惯了宇宙第一IDE(Visual Studio),所以当Visual Studio C ...

  5. C:数据结构与算法之单链表

    单链表相对于顺序表比较难理解,但是比较实用,单链表的插入,删除不需要移动数据元素,只需要一个指针来寻找所需要的元素,还有一个大优点就是不浪费空间,当你想要增加一个结点可以申请(malloc())一个结 ...

  6. 如何在markdown中插入表情

    Markdown是很好用的呀,个人灰常的喜欢,也是灰常漂亮的,但是如何在写文章的时候插入表情呢,下面给出一部分代码

  7. 浅谈传统语音通信和APP语音通信音频软件开发之不同点

    本人在传统的语音通信公司做过手机和IP电话上的语音软件开发,也在移动互联网公司做过APP上的语音软件开发.现在带实时语音通信功能的APP有好多,主流的有微信语音.QQ电话.钉钉等,当然也包括我开发过的 ...

  8. bzoj 1196: [HNOI2006]公路修建问题

    Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织 ...

  9. ArrayList中对象 排序

    public class Student implements Comparable { private String studentname; public int studentage; publ ...

  10. Cleaner, more elegant, and wrong(翻译)

    Cleaner,more elegant,and wrong 整洁,更优雅,但是错的 并不是因为你看不到错误的产生路径就意味着它不存在. 下面是C#编程书中的一个片段,摘自关于异常处理的章节. try ...