HDU 3639 Bone Collector II(01背包第K优解)
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
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.
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.
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
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优解)的更多相关文章
- HDU - 2639 Bone Collector II (01背包第k大解)
分析 \(dp[i][j][k]\)为枚举到前i个物品,容量为j的第k大解.则每一次状态转移都要对所有解进行排序选取前第k大的解.用两个数组\(vz1[],vz2[]\)分别记录所有的选择情况,并选择 ...
- HDU 2639 Bone Collector II(01背包变形【第K大最优解】)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu–2369 Bone Collector II(01背包变形题)
题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...
- HDU2639Bone Collector II[01背包第k优值]
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 2639 Bone Collector II (01背包,第k解)
题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m ...
- HDU 2639 Bone Collector II(01背包变型)
此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- 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个 然后归并排序并 ...
- (01背包 第k优解) Bone Collector II(hdu 2639)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 Problem Description The title of this problem i ...
随机推荐
- [Android Pro] Android Support 包里究竟有什么
reference to : http://www.2cto.com/kf/201411/350928.html 随着 Android 5.0 Lollipop 的发布,Android 又为我们提供了 ...
- Hudson可扩展持续集成引擎
参考文章:http://blog.csdn.net/dazhi_100/article/details/11629133 极限编程中一项建议实践便是持续集成,持续集成是指在开发阶段,对项目进行持续性自 ...
- jQuery 知识点积累
1.判断checkbox是否选中 $("#aa").prop("checked")==true2.给属性赋值 $("#aa").pr ...
- hdu 4578 线段树 ****
链接:点我 1
- HDU 4362 Dragon Ball 贪心DP
Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon ...
- HDU 4348 To the moon 可持久化线段树
To the moon Problem Description BackgroundTo The Moon is a independent game released in November 201 ...
- [MySQL]导入导出
[MySQL]导入导出 一 导入文本数据 1)mysql->load data infile 数据文件c:/mytable.txt 如下:(每一行为一条记录,记录的字段间用tab隔开,最后一个字 ...
- WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
我百度到的答案,原文请点击 错误信息: WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping.请添加一个名为 jquer ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- Quartz.net misfire实践
1.问题描述 在使用Quartz.net定时运行作业时,存在一种情况:作业错过了某次执行,当作业恢复的时候应该怎么处理?如:job1在3:50的时候应该执行的,但此刻job1处于暂停状态,而到3: ...