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 ...
随机推荐
- 3ds Max学习日记(七)
第7章讲的是多边形建模,实例略多,有十六个,再加上周日的怠惰感,只做了几个实例. 附上今日的劳动成果: 布料(创建一个平面,转换为可编辑多边形,然后调整顶点,连接一些边,添加网格平滑,转换为可 ...
- 【Linux】- CentOS 防火墙iptables和firewall
1 iptables防火墙 1.1 基本操作 # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 s ...
- 使用Windows Live Writer拉取之前写的博客
因为之前写的博客有错误需要修改,但是在Windows Live Writer中找了半天也没找到怎么拉取之前的博客,在[打开本地草稿]或者[打开最近使用过的日志]中,由于存储的项数有限,所以就找不到那篇 ...
- Atom IDE开发工具, ASCII艺术评论, ninimap 插件
1 ASCII Art Comments One neat trick is to use ASCII art to create huge comments visible in the minim ...
- [C/C++] 原码、反码、补码问题
正确答案:D 解析: C语言中变量以补码形式存放在内存中,正数的补码与原码相同,负数求补码方式为(符号位不变,其余各位取反,最后末尾加1): 32位机器:int 32位,short 16位. x = ...
- Apache与Tomcat负载均衡
Apache HTTP Server 与 Tomcat 的三种连接方式JK,http_proxy,ajp_proxy.下面逐个介绍一下(本篇介绍的示例都是基于前面介绍的已经搭建好的Tomcat集群,都 ...
- SocketServer-实现并发处理
Python提供了两个基本的socket模块. 一个是socket,它提供了标准的BSD Socket API:另一个是socketServer,它提供了服务器中心类,可以简化网络服务器的开发,其实就 ...
- Spring-Boot基于配置按条件装Bean
背景 同一个接口有多种实现,项目启动时按某种规则来选择性的启用其中一种实现,再具体一点,比如Controller初始化的时候,根据配置文件的指定的实现类前缀,来记载具体Service,不同Servic ...
- BZOJ1293:[SCOI2009]生日礼物——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1293 https://www.luogu.org/problemnew/show/P2564#sub ...
- 从零开始学Linux系统(三)安装CentOS-7及软件包管理操作
推荐博文: VirtualBox安装CentOS7步骤详解: https://my.oschina.net/AaronDMC/blog/840753 如何安装CentOS7字符界面 :http://b ...