题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … 
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背包模板题]的更多相关文章

  1. HDU 2602 Bone Collector(01背包裸题)

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. [HDU 2602]Bone Collector ( 0-1背包水题 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 水题啊水题 还给我WA了好多次 因为我在j<w[i]的时候状态没有下传.. #includ ...

  3. HDU 2602 Bone Collector --01背包

    这种01背包的裸题,本来是不想写解题报告的.但是鉴于还没写过背包的解题报告.于是来一发. 这个真的是裸的01背包. 代码: #include <iostream> #include < ...

  4. HDU 2602 Bone Collector (01背包DP)

    题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...

  5. 解题报告:hdu2602 Bone collector 01背包模板

    2017-09-03 15:42:20 writer:pprp 01背包裸题,直接用一维阵列的做法就可以了 /* @theme: 01 背包问题 - 一维阵列 hdu 2602 @writer:ppr ...

  6. HDU 2602 Bone Collector (01背包问题)

    原题代号:HDU 2602 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 原题描述: Problem Description Many yea ...

  7. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  8. HDU 2602 Bone Collector 骨头收集者【01背包】

    题目链接:https://vjudge.net/contest/103424#problem/A 题目大意: 第一行输入几组数据,第二行第一个数字代表物体个数,第二个数代表总体积.需要注意的是,第三排 ...

  9. HDU 2602 Bone Collector 0/1背包

    题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

随机推荐

  1. ASCII码与16进制的互相转换(表)

    所谓的ASCII和16进制都只是概念上的东西,在计算机中通通是二进制 转换应该是输出的转换,同样是一个数,在计算机内存中表示是一样的,只是输出不一样ASCII是针对字符的编码,几乎是键盘上的字符的编码 ...

  2. IOS端的摇一摇功能

    //微信的摇一摇是怎么实现的~发现原来 ios本身就支持 //在 UIResponder中存在这么一套方法 - (void)motionBegan:(UIEventSubtype)motion wit ...

  3. mongodb安装使用笔记

    mongodb安装使用 安装后配置环境变量 创建数据库文件夹并连接数据库,并执行mongod --dbpath c:\workname 打开新的cmd,执行mongo命令,管理数据库 show dbs ...

  4. ios开发之--复制到剪切板

    仅做记录: UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = @"你好&quo ...

  5. android 网络检测

    这个过程我觉得有必要记录一下事情的起因是这样的, 写的程序在虚拟机下面无法连接到服务器,首先想到的是,虚拟机能不能访问外网,打开某搜索网站,正常,想用ping命令来ping服务器,于是就有了下面的过程 ...

  6. Python爬虫学习笔记-2.Requests库

    Requests是Python的一个优雅而简单的HTTP库,它比Pyhton内置的urllib库,更加强大. 0X01 基本使用 安装 Requests,只要在你的终端中运行这个简单命令即可: pip ...

  7. 自己搭建CDN服务器静态内容加速-LuManager CDN使用教程

    为什么要自己来搭建一个CDN服务器实现网站访问加速?一是免费CDN服务稳定性和加速效果都不怎么行:二是用国内的付费CDN服务价格贵得要死,一般的草根站长无法承受:三是最现实的问题国内的CDN要求域名B ...

  8. #pragma init_seg

    先进后出原则,最先初始化的最后析构! 1.C++中全局对象.变量的构造函数调用顺序是跟声明有一定关系的,即在同一个文件中先声明的先调用.对于不同文件中的全局对象.变量,它们的构造函数调用顺序是未定义的 ...

  9. codeforces水题100道 第二十四题 Codeforces Beta Round #85 (Div. 2 Only) A. Petya and Strings (strings)

    题目链接:http://www.codeforces.com/problemset/problem/112/A题意:忽略大小写,比较两个字符串字典序大小.C++代码: #include <cst ...

  10. JS - 点击事件排除父级标签

    点击事件排除父级标签,这里使用的是stopPropagation()方法.event.stopPropagation(); 对了,这里还用了解除click事件,unbind. 下面这篇博文,介绍挺全的 ...