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 ...
随机推荐
- iOS- <项目笔记>iOS6 & iOS7屏幕图片适配
1.为非视网膜\视网膜屏幕分别准备2份图片,比如: 1> 非视网膜 abc.png 2> 视网膜 abc@2x.png 程序检测视网膜屏到会自动替换@2x 2.程序启动图片 * 程序启动过 ...
- iOS开发应用程序生命周期
各个程序运行状态时代理的回调: - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSD ...
- Zigbee安全基础篇Part.2
原文地址: https://www.4hou.com/wireless/14252.html 导语:本文将会探讨ZigBee标准提供的安全模型,用于安全通信的各种密钥.ZigBee建议的密钥管理方法以 ...
- JSTL标签之核心标签
JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个实现 Web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断.数据管理格式化.XML 操作以及数 ...
- perf使用的问题,再看perf record,perf record 设置的采样频率,采样频率是如何体现在
当perf stat -e branches 是统计 再看perf record,perf record是为了是记录时间发生的时候的调用栈, 在我的测试代码中总共有200,000,000条branch ...
- 发送tcp的时候,数据包是如何拷贝的
发送数据包的时候,用户态的数据包是如何拷贝到内核的kiovec msghd 结构体 icmp是走sock吗? 每一个skb_buffer的大小都是固定的吗?所以有skb_available这样的函数 ...
- 【转】log4j.properties文件的配置
一.前言 log4j使用的还是比较多的,但是对于其配置又很难描述清楚要怎么配置,说明我自己对于log4j的配置并不是非常熟悉,所以在网上找了一篇详尽的 博文转载,在此非常感谢原文作者的辛苦付出,如有需 ...
- css3 字体渐变
先看个效果 https://www.bienvillecapital.com/ 然后人家样式这样写的 font-family: Overpass,Helvetica,sans-serif; font- ...
- iphone手机与PC蓝牙出现感叹号且无法修复解决方案
解决方案如下: 1.需要下载Windows Mobile 6.5 的驱动 drvupdate-amd64.exe ,下载需要正版验证,手动安装驱动,具体步骤Google 2. 如果在BlueTooth ...
- MySQL安装出现“不是内部或外部命令,也不是可执行程序”等一系列问题的解决方案
MySQL安装出现“不是内部或外部命令,也不是可执行程序” 一.这是应该是环境变量处问题了,设置如下: 1)右击我的电脑选择“属性”,找到“高级系统设置” 2)在系统属性下,选择“高级”中的“环境变量 ...