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) ...
随机推荐
- ios开发之--时间格式化
字符串转时间 iOS 中 NSDate 是时间管理类,这里获取到的时间是 UTC 时间,可以根据需要转换成任意地区的时间. NSFormatter 类是时间格式化类,可以根据指定的格式.地区将指定的时 ...
- 【ArcGIS】ArcGIS Enterprise部署
单机部署 多层部署 高可用性部署
- linux给当前用户添加环境变量
比如当前用户为oracel,则添加环境变量操作为: vim /home/oracel/.bashrc
- 【代码审计】iCMS_v7.0.7 admincp.app.php页面存在SQL注入漏洞分析
0x00 环境准备 iCMS官网:https://www.icmsdev.com 网站源码版本:iCMS-v7.0.7 程序源码下载:https://www.icmsdev.com/downloa ...
- 【代码审计】MIPCMS 远程写入配置文件Getshell
0x00 环境准备 MIPCMS官网:https://www.mipcms.cn 网站源码版本:MIPCMS内容管理系统 V3.1.0(发布时间:2018-01-01) 程序源码下载:http://w ...
- VisualSVN破解
先讲下破解原理 首先,去VisualSVN官网下载最新版本. 传送门:http://www.visualsvn.com/server/download/ 定位到VisualSVN安装目录,C:\Pro ...
- Ansible 如何查看模块文档
[root@localhost ~]$ ansible-doc -l # 列出所有模块 [root@localhost ~]$ ansible-doc cron # 查看指定模块的文档
- urllib 基础模块
(1) urllib.request:最基本的HTTP请求模块,用来模拟发送请求,就像在浏览器里输入网址然后回车一样(2) urllib.error:异常处理模块,如果出现请求错误,我们可以捕获这些异 ...
- osgearth缓存数据命令
新建一个.bat文件 中国地区 osgearth_package.exe ReadyMap.earth --tms ReadyMap.earth --out D:\APICenter\EarthDat ...
- ida+windbg调试windows
jpg 改 pdf https://www.hex-rays.com/products/ida/support/tutorials/debugging_windbg.pdf