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;
}

最新文章

  1. Kafka 文档引言
  2. 通过代码自定义cell(cell的高度不一致,比如微博)
  3. mac 设置阿里企业邮箱
  4. MFC 文件对话框
  5. 说说lambda表达式与表达式树(未完)
  6. iOS 中通过使用Google API获得Google服务
  7. oracle中清空表数据的两种方法
  8. 2.精通前端系列技术之JS模块化开发-深入学习seaJs(四)
  9. hdu 4712
  10. zTree实现清空选中的第一个节点的子节点
  11. c#核心基础 - 浅谈 c# 中的特性 Attribute)
  12. 创建pandas和sqlalchemy的j交互对象,方便于日常的数据库的增删改查(原创)
  13. [Swift]LeetCode44. 通配符匹配 | Wildcard Matching
  14. html/css 表格元素以及表格布局
  15. JVM源码分析--ClassLoader类加载器
  16. Dynamic CRM 2015学习笔记(4)修改开发人员资源(发现服务、组织服务和组织数据服务)url地址及组织名
  17. 【读书笔记】iOS-Web应用程序的自动化测试
  18. subprocess.Popen指令包含中文导致乱码问题解决
  19. bean标签常用属性
  20. [技术选型] Node.js

热门文章

  1. 爬虫 之 scrapy-redis组件
  2. Native2Ascii文件转换 -- 待完善
  3. Storm中并行度原来是这样计算的(1.0.1版本)
  4. linux centOS下怎么配置web服务器
  5. Perl 学习笔记-模块
  6. [GO]方法集
  7. 免秘钥oracel官方下载jdk
  8. Spring框架总结(十)
  9. 大佬福利之在你眼中 Web 3.0 是什么?(转)
  10. struts2 和 js 标签取值