HDU 2602 - Bone Collector - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the
maximum of the total value the bone collector can get ?
InputThe 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, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. 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.
OutputOne integer per line representing the maximum of the total value (this number will be less than 2 31).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
题解:
01背包模板题。
AC代码:
二维数组写法:
#include<cstdio>
#include<algorithm>
using namespace std; unsigned long max_weight[][];//max_weight[i][j]:前i个骨头中,容量为j的背包里能放的下的最大重量
struct type{
int v;
int w;
}bone[]; int main()
{
int n,V;
int t;scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&V);
for(int i=;i<=n;i++) scanf("%d",&bone[i].w);
for(int i=;i<=n;i++) scanf("%d",&bone[i].v); for(int j=;j<=V;j++){
if(bone[].v <= j) max_weight[][j]=bone[].w;
else max_weight[][j]=;
} for(int i=;i<=n;i++){
for(int j=;j<=V;j++){
if(j<bone[i].v) max_weight[i][j]=max_weight[i-][j];
else max_weight[i][j]=max( max_weight[i-][j] , max_weight[i-][ (j-bone[i].v) ] + bone[i].w );
}
} printf("%d\n",max_weight[n][V]);
}
}
减小空间复杂度,滚动一维数组写法:
#include<cstdio>
#include<algorithm>
using namespace std; unsigned long max_weight[];
struct type{
int v;
int w;
}bone[]; int main()
{
int n,V;
int t;scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&V);
for(int i=;i<=n;i++) scanf("%d",&bone[i].w);
for(int i=;i<=n;i++) scanf("%d",&bone[i].v); for(int j=;j<=V;j++){
if(bone[].v <= j) max_weight[j]=bone[].w;
else max_weight[j]=;
} for(int i=;i<=n;i++){
for(int j=V;j>=;j--){
if(j<bone[i].v) max_weight[j]=max_weight[j];
else max_weight[j]=max( max_weight[j] , max_weight[ (j-bone[i].v) ] + bone[i].w );
}
} printf("%d\n",max_weight[V]);
}
}
空间复杂度的对比:
HDU 2602 - Bone Collector - [01背包模板题]的更多相关文章
- HDU 2602 Bone Collector(01背包裸题)
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- [HDU 2602]Bone Collector ( 0-1背包水题 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 水题啊水题 还给我WA了好多次 因为我在j<w[i]的时候状态没有下传.. #includ ...
- HDU 2602 Bone Collector --01背包
这种01背包的裸题,本来是不想写解题报告的.但是鉴于还没写过背包的解题报告.于是来一发. 这个真的是裸的01背包. 代码: #include <iostream> #include < ...
- HDU 2602 Bone Collector (01背包DP)
题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...
- 解题报告:hdu2602 Bone collector 01背包模板
2017-09-03 15:42:20 writer:pprp 01背包裸题,直接用一维阵列的做法就可以了 /* @theme: 01 背包问题 - 一维阵列 hdu 2602 @writer:ppr ...
- HDU 2602 Bone Collector (01背包问题)
原题代号:HDU 2602 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 原题描述: Problem Description Many yea ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- HDU 2602 Bone Collector 骨头收集者【01背包】
题目链接:https://vjudge.net/contest/103424#problem/A 题目大意: 第一行输入几组数据,第二行第一个数字代表物体个数,第二个数代表总体积.需要注意的是,第三排 ...
- HDU 2602 Bone Collector 0/1背包
题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- Shiro集成Spring
本篇博客主要讲述的是两者的集成.不涉及到各自的详细细节和功能. 因为官方给出的文档不够具体,对新手而言通过官方文档还不可以非常快的搭建出SpringShiro的webproject.本博客将通过实际的 ...
- Ubuntu 12.04下安装QQ 2012 Beta3(转)
Ubuntu 12.04下安装QQ 2012 Beta3 由于wine的发展非常迅速.现在网上的利用老版本的wine来安装QQ2012的教程已经有些过时了.实际上操作起来非常简单: 第一步:Ctr ...
- WSGI简介
当我们实现一个Web应用(application)的时候,通常不会考虑如何接受HTTP请求.解析HTTP请求.发送HTTP响应等等,我们只关心处理逻辑,而不用去关心HTTP规范的细节. 之所以有这层透 ...
- Python对象(下)
前面一篇文章介绍了一些Python对象的基本概念,这篇接着来看看Python对象相关的一些内容. Python对象的比较 Python对象有三个要素:身份,类型和值,所以我们就分别从这三个角度出发看看 ...
- ios开发之--打印bool值
eg:NSLog(@"Hello,objective-c!"); @表示应该当作NSString字符串来处理. NSLog相当于C语言中的printf,常用于文字输出 NSLo ...
- C mysql (C API Commands out of sync; you can't run this command now)
错误出现在当一个用户使用查询,另一个用户再使用此sql连接进行查询的时候: 原因是因为上一次使用此sql连接进行查询时没有将所有的结果集给释放掉,在所有使用此sql连接进行查询的地方将所有的结果集给释 ...
- SaltStack 批量分发目录
这里演示如何将 salt-master 上的目录批量分发到多台 salt-minion,步骤如下: [root@localhost ~]$ cat /srv/salt/top.sls # 先定义入口配 ...
- 使用 requests 进行身份认证
如下图,有些网站需要使用用户名密码才可以登录,我们可以使用 requests 的 auth 参数来实现 import requests req = requests.get("http:// ...
- Zabbix的自定义键值和自动发现功能监控Oracle数据库的表空间
前面介绍了利用Orabbix监控了,参考zabbix通过Orabbix监控oracle数据库,这里我们原先的模板中进行了修改,使用自动发现功能实现监控tablespace的使用情况. 1. 在被监控的 ...
- Mock
转移到 这里 像测试对象提供一套和测试资源完全相同 的接口和方法,不关心过程,只关心结果(比如模拟拿到服务器的code)