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) ...
随机推荐
- Nginx: error while loading shared libraries: libpcre.so.1解决
Shell代码 [root@tmsapp65 conf]# /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx: error while l ...
- eclipse中设置文件的编码格式为utf-8
1.可以在 eclipse 中配置 workspace 项下 text file encoding 属性的值来决定此工作区间下所有的 eclipse 项目的文档的编码属性. Window-->P ...
- mysql强制使用索引
在公司后台某模块功能记录日志中有一个搜索功能,通过前段时间的产品使用时间区间进行搜索反馈有些卡顿,我发现这个搜索功能比较慢,要3秒左右才能出来,就决定对这里做一下优化. 通过分析代码和SQL发现最核心 ...
- 删除ORACLE目录OCI.dll文件无法删除 (转)
删除ORACLE目录OCI.dll文件无法删除 今天准备把虚拟机里的10g卸载安装11g来研究一些新特性 卸载没有用自带的UnInstall工具之前看warehouse的讲课视频凭记忆手动卸载了下删除 ...
- mongodb命令(1)
成功启动MongoDB服务后,打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显 ...
- 流媒体服务器+EasyDarwin+EasyPusher+VLC+Red5+OBS+Unity+RTSP+RTMP+FFMPEG
最近有个需求在Unity中直播桌面,着用到了视频流. ------------------------------ VLC自身有流服务器功能,但是非常慢非常慢,还是用VLC拉流吧,好像大家也是这么做的 ...
- [Ubuntu] 如何设置静态 IP 和 DNS
编辑 /etc/network/interfaces 来设置 IP 和 DNS 解析服务器: # interfaces() ) and ifdown() auto lo iface lo inet l ...
- activity的启动模式有哪些?
Activity启动模式设置: <activity android:name=".MainActivity" android:launchMode="standar ...
- SQL Server 索引结构及其使用(二)
作者:freedk 一.深入浅出理解索引结构 改善SQL语句 很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select ...
- linux计划任务之crontab
语法: crontab [ -u user ] file crontab [ -u user ] [ -i ] { -e | -l | -r } 说明: crontab命令 ...