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 ...
 
随机推荐
- Cocos2d-JS的屏幕适配方案
			
Cocos2d引擎为游戏开发者提供了屏幕适配策略(Resolution Policy)解决方案. 使用方式 1. 设置屏幕适配策略(Resolution Policy) 如果你还没有用过Resolut ...
 - POJ1704 Georgia and Bob (阶梯博弈)
			
Georgia and Bob Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Subm ...
 - ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(二) 实现聊天室连接
			
上一篇已经简单介绍了layim WebUI即时通讯组件和获取数据的后台方法.现在要讨论的是SingalR的内容,之前都是直接贴代码.那么在贴代码之前先分析一下业务模型,顺便简单讲一下SingalR里的 ...
 - Redis适用于高并发的递增、递减功能
			
递增指令:incr(默认从0开始) 递减指令:decr(默认从0开始,递减会出现负数,这点跟memcache不一样,mc到0) 如下: 附上shardedJedisPool和JedisCluster的 ...
 - 在qq中可以使用添加标签功能
			
而在sina中不可以,现在就保持一致吧!那么每天使用的日志主要是记录工作项目上的问题还有生活的感受
 - Nginx开启Gzip压缩大幅提高页面加载速度
			
[root@12 conf]# vi nginx.conf gzip on;#开启GZIP gzip_min_length 1k; #不压缩临界值,大于1K的才压缩,一般不用改 gzip_buffer ...
 - JUC回顾之-CyclicBarrier底层实现和原理
			
1.CyclicBarrier 字面意思是可循环(Cyclic)使用的屏障(Barrier).它要做的事情是让一组线程到达一个屏障(同步点)时被阻塞,直到最后一个线程到达屏障时候,屏障才会开门.所有被 ...
 - ***Linux文件夹文件创建、删除、改名
			
Linux删除文件夹命令 linux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm ...
 - POJ 3461 Oulipo   KMP
			
题意:统计其中一个子串的出现次数 题解:即KMP算法中j==m的次数 //作者:1085422276 #include <cstdio> #include <cmath> #i ...
 - 电赛菜鸟营培训(三)——STM32F103CB之串口通信
			
一.串口通信概念 1.缩写 USART:Universal Synchronous/Asynchronous Receiver/Transmitter 通用同步/异步接收和发送器 2.用处 (1)同步 ...