HDU 2639 Bone Collector II (dp)
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
分析:
简单解释一下题目要求。输入给你n中物品,背包的容量(maxvolum)和一个整数k
求在maxvolum的限制条件下所能得到的第k大的价值
这是一个很典型的第k优解的问题
首先看普通01背包的状态转移方程 : fi = max( fi-1,fi] + val[i])。
如果要求第k优解,那么应该设定另一个状态转移方程: fi[k]表示在j背包容量限制下,前i个物品所能获得的第k大价值。
然后原方程就可以解释为:fi这个有序列是由fi-1和fi-1]+val[i]这两个有序队列合并(合并的方式是通过max求取两个钟的较大的值)得到的。
有序列fi-1即fi-1[1..K],fi-1]+val[i]则理解为在fi-1][1..K]的每个数上加上val[i]后得到的有序列。
那么fi[k]就是上述两个有序列(也是通过max求两个较大的那个)合并得到
转换到代码当中去,我们需要找两个数组chose[i] 和 not_chose[i]来存储两个序列(每一个序列其实是由一系列的状态构成的)
最后将这两个数组合并即有了第k(k from 1 to k)优解的序列.
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<set>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int N,V,K,w[1003],v[1003],dp[1003][33]= {0},c[1003];
scanf("%d%d%d",&N,&V,&K);
for(int i=0; i<N; i++)
scanf("%d",&w[i]);
for(int i=0; i<N; i++)
scanf("%d",&v[i]);
for(int i=0; i<N; i++)
for(int j=V; j>=v[i]; j--)
{
int k1=0;
for(int t=0; t<K; t++)
{
c[k1++]=dp[j][t];
c[k1++]=dp[j-v[i]][t]+w[i];
}
sort(c,c+K*2,cmp);
k1=1;
dp[j][0]=c[0];
for(int t=1; t<K*2&&k1<K; t++)
if(c[t]!=c[t-1])
dp[j][k1++]=c[t];
}
printf("%d\n",dp[V][K-1]);
}
return 0;
}
HDU 2639 Bone Collector II (dp)的更多相关文章
- 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 2639 Bone Collector II (01背包,求第k优解)
这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...
- HDU 3639 Bone 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大最优解】)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 2639 Bone Collector II
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 2639 Bone Collector II(01背包变型)
此题就是在01背包问题的基础上求所能获得的第K大的价值. 详细做法是加一维去推当前背包容量第0到K个价值,而这些价值则是由dp[j-w[ i ] ][0到k]和dp[ j ][0到k]得到的,事实上就 ...
- 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 ...
- HDU 2639 Bone Collector II (01背包,第k解)
题意: 数据是常规的01背包,但是求的不是最大容量限制下的最佳解,而是第k佳解. 思路: 有两种解法: 1)网上普遍用的O(V*K*N). 2)先用常规01背包的方法求出背包容量限制下能装的最大价值m ...
- HDU - 2639 Bone Collector II 题解
题目大意 一个人收藏骨头,有 n 个骨头,每个骨头有体积和价值,问能够装在容量为 V 的背包中,能获得的第 k 大(去重后)价值是多少. 样例 样例输入 1 5 10 2 1 2 3 4 5 5 4 ...
随机推荐
- JDK源码分析 – LinkedList
LinkedList类的申明 public class LinkedList<E> extends AbstractSequentialList<E> implements L ...
- TFS持续集成
TFS持续集成的就是跟踪代码变更,合并,能够自定义脚本,任务进行自动化测试,发版,部署,有点像docker的味道.在这个代理服务器分布式中tfsserver起着能够随时拿去最新代码能够统一执行任务的角 ...
- css3边框阴影效果
下面来说下css3阴影的语法: box-shadow:none | <shadow> [ , <shadow> ]* <shadow> = inset? & ...
- 在 Range 对象中,Min (14)必须小于或等于 max (-1)。
DataTable dt = ds.Tables[]; DataRow[] drs = dt.Select("Id=" + categoryID ); 解决方法:将参数用单引号阔起 ...
- cacti安装spine 解决WARNING: Result from CMD not valid. Partial Result: U错误
安装spine用来替换cacti原本的执行方式,需要的包在附件中,请注意spine的安装包和你安装的cacti版本不用相同,最好是最新的spine 1.安装gcc #yum install -y gc ...
- mysql+navicat安装小结
1,mysql到官方下载,navicat下载破解版 2,修改my.ini, 注意,需要手动创建data文件夹, 其中C:\MySql\mysql-5.7.17-winx64是解压mysql的目录 [m ...
- KeyPress 和KeyDown 、KeyPress之间的区别
虽然从字面理解, KeyDown是按下一个键的意思, 但实际上二者的根本区别是, 系统由KeyDown返回键盘的代码, 然后由TranslateMessage函数翻译成成字符, 由KeyPress返回 ...
- Redis源码剖析
Redis源码剖析和注释(一)---链表结构 Redis源码剖析和注释(二)--- 简单动态字符串 Redis源码剖析和注释(三)--- Redis 字典结构 Redis源码剖析和注释(四)--- 跳 ...
- 【bzoj1727】[Usaco2006 Open]The Milk Queue 挤奶队列 贪心
题目描述 Every morning, Farmer John's N (1 <= N <= 25,000) cows all line up for milking. In an eff ...
- SocketServer-实现并发处理
Python提供了两个基本的socket模块. 一个是socket,它提供了标准的BSD Socket API:另一个是socketServer,它提供了服务器中心类,可以简化网络服务器的开发,其实就 ...