hdu-2639

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块骨头,V容量的背包,把骨头放进包里,求价值第k大时是多少。

思路

就是个01背包的变种,根据dp思想,在01背包的基础上加多一个维度,dp[j][k]表示容量为j的背包下,第k大的价值。

首先考虑第1大的数,是max(dp[i][j], dp[i - 1][j - c[i]] + w[i])

可以推断,第k大的值可以在两组数dp[i][j][z]、dp[i - 1][j - c[i]][z] + w[i],z∈[1, ... , k]中得到

然而并不能直接知道这两组数中前k大的数,所以将dp[i][j][z]放入A[],将dp[i - 1][j - c[i]][z] + w[i]放入B[]

然后将A[]、B[]两组数一起排序,就能得到第k大的数

代码

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstdio> using namespace std; typedef long long LL;
const int N = 1005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7; int main()
{
int T;
cin >> T;
while(T--)
{
LL n, V, K;
cin >> n >> V >> K;
LL w[N], c[N];
for(int i = 1;i <= n;++i)
cin >> w[i];
for(int i = 1;i <= n;++i)
cin >> c[i]; LL dp[N][35];
memset(dp, 0, sizeof(dp));
for(int i = 1;i <= n;++i)
{
for(int j = V;j >= c[i];--j)
{
LL A[35], B[35];
int a, b, num;
for(int k = 1;k <= K;++k)
{
A[k] = dp[j - c[i]][k] + w[i];
B[k] = dp[j][k];
}
A[K + 1] = B[K + 1] = -1;//-1 < 0。a <= K做判断条件会出错
a = b = num = 1;
while(num <= K && (A[a] != -1 || B[b] != -1))
{
if(A[a] > B[b])
dp[j][num] = A[a++];
else
dp[j][num] = B[b++];
if(dp[j][num] != dp[j][num - 1])
num++;
}
}
}
cout << dp[V][K] << endl;
}
return 0;
}

01背包--hdu2639的更多相关文章

  1. dp之01背包hdu2639(第k优解)

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值.......(注意,不要 和它的第一题混 ...

  2. HDU2639(01背包第K大)

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

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

    题目http://acm.hdu.edu.cn/showproblem.php?pid=2639 分析:这是求第K大的01背包问题,很经典.dp[j][k]为背包里面装j容量时候的第K大的价值. 从普 ...

  4. NO11——01背包

    # include <stdio.h> # include <stdlib.h> # include <string.h> # define max(x,y) x& ...

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

  6. UVALive 4870 Roller Coaster --01背包

    题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F ,     D -= K 问在D小于等于一定限度的时 ...

  7. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  8. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  9. 51nod1085(01背包)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...

随机推荐

  1. 让你大开眼界的10款Android界面设计

    根据调查显示, iOS与Android的市场份额差距正越来越大.Android设备正在成为手机应用市场的主力军.如何从设计层面创造一个优美的app界面来吸引用户已然成为广大App开发者们必做的功课之一 ...

  2. Win 7 Windows Update无法自动更新解决方案

    最近发现系统很长时间没有自动更新过了,手动更新后,提示返回错误码WindowsUpdate_8024402F.网络上搜索到的解决方法大多是删除更新临时目录,重启WINDOWS UPDATE服务,然而试 ...

  3. Go 面向对象概念

    前言: 本文是学习<<go语言程序设计>> -- 清华大学出版社(王鹏 编著) 的2014年1月第一版 做的一些笔记 , 如有侵权, 请告知笔者, 将在24小时内删除, 转载请 ...

  4. mongodb 查询条件

    这节来说说mongodb条件操作符,"$lt", "$lte", "$gt", "$gte", "$ne&qu ...

  5. javascript总结26:Date

    1 获取Date对象 Date-引用类型,JavaScript中的内置对象 获取当前时间 var date = new Date(); //UTC的时间 //返回数字,时间的毫秒形式 var date ...

  6. CSS中的三种基本的定位机制(普通流、定位、浮动)

    一.普通流 普通流中元素框的位置由元素在XHTML中的位置决定.块级元素从上到下依次排列,框之间的垂直距离由框的垂直margin计算得到.行内元素在一行中水平布置. 普通流就是html文档中的元素如块 ...

  7. 洛谷P2147[SDOI2008]洞穴勘测(lct)

    题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...

  8. Mac OS X Yosemite & Arduino安装CH340 USB转串口驱动

    新买的Arduino开发板 USB转串口使用了CH340芯片,在Mac OS X Yosemite上正常安装驱动后,在Arduino IDE的端口没发现相应的设备,使用以下方法后就能使用USB转串口调 ...

  9. ios系统架构及常用框架

    1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch layer).媒体层(Media l ...

  10. LoadRunner10个用户并发测试时分别取不同的参数运行脚本

    使用场景,比如说10个用户使用不同的账户名和密码同时并发登录,此时选择如下参数化策略: 参数选择:select next row 选择unique update value on 选择 once 另一 ...