Bone Collector II

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

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
 

题目链接:HDU 2639

用in[]记录取第i的物品的答案,用out[]记录不取的答案,然后从in与out中寻找第1~k个值,放入dp[v][k]中……由于in与out至少在k范围内均是单调不增的序列,那只要判断一下重复的即可,相当于01背包多了个过程记录

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=110;
const int K=35;
int w[N],c[N];
int in[K],out[K];
int dp[N*10][K];
void init()
{
CLR(in,0);
CLR(out,0);
CLR(dp,0);
}
int main(void)
{
int n,v,k,i,j,q;
int tcase;
scanf("%d",&tcase);
while (tcase--)
{
scanf("%d%d%d",&n,&v,&k);
init();
for (i=0; i<n; ++i)
scanf("%d",&w[i]);
for (i=0; i<n; ++i)
scanf("%d",&c[i]);
for (i=0; i<n; ++i)
{
for (j=v; j>=c[i]; --j)
{
for (q=1; q<=k; ++q)
{
in[q]=dp[j-c[i]][q]+w[i];
out[q]=dp[j][q];
}
int a=1,b=1,c=1;
in[k+1]=out[k+1]=-INF;
while (c<=k&&(in[a]!=-INF||out[b]!=-INF))
{
if(in[a]>out[b])
dp[j][c]=in[a++];
else
dp[j][c]=out[b++];
if(dp[j][c]!=dp[j][c-1])
++c;
}
}
}
printf("%d\n",dp[v][k]);
}
return 0;
}

HDU 3639 Bone Collector II(01背包第K优解)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  8. 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个 然后归并排序并 ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2639       Problem Description The title of this problem i ...

随机推荐

  1. 6个朋友(codevs 2832)

    2832 6个朋友  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有这么一种说法:认识6个人,你就认识全世 ...

  2. select into from 提示 Undeclared variable.....错误的解决办法 && select into from 和 insert into select 的用法和区别

    然而今天在使用 SELECT INTO FROM 备份mysql数据表的时候,运行相关 sql 语句的时候却一直返回 [Err] 1327 - Undeclared variable: ...... ...

  3. HDU 2819 — Swap 二分匹配

    Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. msconfig设置调试开启 关闭 操作注册表项是

    经过测试   9dea862c-5cdd-4e70-acc1-f32b644d4795  这个项每个系统都是固定的.这个项里面的  Elements 里面项也是固定的.在 24000001 项里的 E ...

  5. CC2540开发板学习笔记(一)——LED点亮

    一.实验内容: 点亮LDE1.2 二.实验原理: 1.电路原理图: 就一个发光二极管串联一个电阻.电阻是为了防止电流过大.利用发光二极管的单向导电性,在P1为高电平是点亮LED,在低电平是熄灭LED. ...

  6. Centos升级内核 --已验证

    Docekr 对内核由要求,3.10以上 Centos 6.5内核达不到要求 [linux@localhost Desktop]$ sudo service docker statusdocker d ...

  7. 1076 K尾相等数

    时间限制:500MS  内存限制:65536K提交次数:251 通过次数:80 题型: 编程题   语言: C++;C Description 从键盘输入一个自然数K(99999999>K> ...

  8. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  9. 金子上的友情[XDU1011]

    Problem 1011 - 金子上的友情 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 336  Ac ...

  10. TYVJ P1037 阶乘统计2 Label:坑

    描述 n的阶乘定义为n!=1*2*3*……*n 如3!=6 n!通常最后会有很多0,如5!=120 最后有一个0,现在统计n!去除末尾的0后,最后k位是多少  输入格式  第一行包括两个数n,k  输 ...