题目链接

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

分析:

简单解释一下题目要求。输入给你n中物品,背包的容量(maxvolum)和一个整数k

求在maxvolum的限制条件下所能得到的第k大的价值

这是一个很典型的第k优解的问题

首先看普通01背包的状态转移方程 : fi = max( fi-1,fi] + val[i])。

如果要求第k优解,那么应该设定另一个状态转移方程: fi[k]表示在j背包容量限制下,前i个物品所能获得的第k大价值。

然后原方程就可以解释为:fi这个有序列是由fi-1和fi-1]+val[i]这两个有序队列合并(合并的方式是通过max求取两个钟的较大的值)得到的。

有序列fi-1即fi-1[1..K],fi-1]+val[i]则理解为在fi-1][1..K]的每个数上加上val[i]后得到的有序列。

那么fi[k]就是上述两个有序列(也是通过max求两个较大的那个)合并得到

转换到代码当中去,我们需要找两个数组chose[i] 和 not_chose[i]来存储两个序列(每一个序列其实是由一系列的状态构成的)

最后将这两个数组合并即有了第k(k from 1 to k)优解的序列.

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<set>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int N,V,K,w[1003],v[1003],dp[1003][33]= {0},c[1003];
scanf("%d%d%d",&N,&V,&K);
for(int i=0; i<N; i++) scanf("%d",&w[i]);
for(int i=0; i<N; i++)
scanf("%d",&v[i]);
for(int i=0; i<N; i++)
for(int j=V; j>=v[i]; j--)
{
int k1=0;
for(int t=0; t<K; t++)
{
c[k1++]=dp[j][t];
c[k1++]=dp[j-v[i]][t]+w[i];
}
sort(c,c+K*2,cmp);
k1=1;
dp[j][0]=c[0];
for(int t=1; t<K*2&&k1<K; t++)
if(c[t]!=c[t-1])
dp[j][k1++]=c[t];
}
printf("%d\n",dp[V][K-1]);
}
return 0;
}

HDU 2639 Bone Collector II (dp)的更多相关文章

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

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

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

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

  3. HDU 3639 Bone Collector II(01背包第K优解)

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

  4. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

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

  5. hdu 2639 Bone Collector II

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

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

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

  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 (01背包,第k解)

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

  9. HDU - 2639 Bone Collector II 题解

    题目大意 一个人收藏骨头,有 n 个骨头,每个骨头有体积和价值,问能够装在容量为 V 的背包中,能获得的第 k 大(去重后)价值是多少. 样例 样例输入 1 5 10 2 1 2 3 4 5 5 4 ...

随机推荐

  1. java-自定义标签&&JSTL标签库详解

    自定义标签是Jav aWeb的一部分非常重要的核心功能,我们之前就说过,JSP规范说的很清楚,就是Jsp页面中禁止编写一行Java代码,就是最好不要有Java脚本片段,下面就来看一下自定义标签的简介: ...

  2. JAVA学习之HashCode

    public native int hashCode(); 返回该对象的哈希码值.支持此方法是为了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能. 一.HashCode ...

  3. asp.net MVC4在Action间跳转 RedirectToAction 传值参数问题

    return RedirectToAction("Test", new { cw = cw, firstdirectoryid = firstdirectoryid }); 上式中 ...

  4. 获取当前路径下的所有文件路径 :listFiles

    获取当前路径下的所有文件路口 :listFiles

  5. 【bzoj3297】[USACO2011 Open]forgot STL+dp

    题目描述 发生了这么多,贝茜已经忘记了她cowtube密码.然而,她记得一些有用的信息. 首先,她记得她的密码(记为变量P)长度为L(1 <= L<=1,000)字符串,并可以被分成 一个 ...

  6. 【bzoj1369】[Baltic2003]Gem 树形dp

    题目描述 给出一棵树,要求你为树上的结点标上权值,权值可以是任意的正整数 唯一的限制条件是相临的两个结点不能标上相同的权值,要求一种方案,使得整棵树的总价值最小. 输入 先给出一个数字N,代表树上有N ...

  7. BZOJ4820 SDOI2017硬币游戏(概率期望+高斯消元+kmp)

    容易想到的做法是建出AC自动机,高斯消元.然而自动机上节点数量是nm的. 注意到我们要求的变量只有n个,考虑将其他不用求的节点合并为一个变量.这个变量即表示随机生成一个串,其不包含任何一个模板串的概率 ...

  8. 用select模拟一个socket server成型版2

    1.字典队列测试 import queue msg_dic={} msg_dic[1]=queue.Queue() msg_dic[1].put('hello') msg_dic[1].put('bo ...

  9. Socket_SSH-2(大文件的一次传输)

    import socket,os server=socket.socket() server.bind(('localhost',9999)) server.listen() while True: ...

  10. Oracle 验证A表的2个字段组合不在B表2个字段组合里的数据

    select id, name from TAB_A t where not exists (select 1 from TAB_B t1 where t.id = t1.id and t.name ...