HDU 2602 Bone Collector (01背包问题)
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 ?
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.
total value (this number will be less than 231).
- # include <stdio.h>
- # include <string.h>
- # include <stdlib.h>
- # include <iostream>
- # include <fstream>
- # include <vector>
- # include <queue>
- # include <stack>
- # include <map>
- # include <math.h>
- # include <algorithm>
- using namespace std;
- # define pi acos(-1.0)
- # define mem(a,b) memset(a,b,sizeof(a))
- # define FOR(i,a,n) for(int i=a; i<=n; ++i)
- # define For(i,n,a) for(int i=n; i>=a; --i)
- # define FO(i,a,n) for(int i=a; i<n; ++i)
- # define Fo(i,n,a) for(int i=n; i>a ;--i)
- typedef long long LL;
- typedef unsigned long long ULL;
- int a[+],b[+],dp[+][+];
- int main()
- {
- int t,n,v;
- cin>>t;
- while(t--)
- {
- cin>>n>>v;
- FOR(i,,n)cin>>b[i];
- FOR(i,,n)cin>>a[i];
- mem(dp,);
- FOR(i,,n)FOR(j,,v)
- {
- if(a[i]<=j)dp[i][j]=max(dp[i-][j],dp[i-][j-a[i]]+b[i]);
- else dp[i][j]=dp[i-][j];
- }
- cout<<dp[n][v]<<endl;
- }
- return ;
- }
解法二:时间复杂度O(n2),空间复杂度O(n)
- # include <stdio.h>
- # include <string.h>
- # include <stdlib.h>
- # include <iostream>
- # include <fstream>
- # include <vector>
- # include <queue>
- # include <stack>
- # include <map>
- # include <math.h>
- # include <algorithm>
- using namespace std;
- # define pi acos(-1.0)
- # define mem(a,b) memset(a,b,sizeof(a))
- # define FOR(i,a,n) for(int i=a; i<=n; ++i)
- # define For(i,n,a) for(int i=n; i>=a; --i)
- # define FO(i,a,n) for(int i=a; i<n; ++i)
- # define Fo(i,n,a) for(int i=n; i>a ;--i)
- typedef long long LL;
- typedef unsigned long long ULL;
- int a[+],b[+],dp[+];
- int main()
- {
- int t,n,v;
- cin>>t;
- while(t--)
- {
- int ans=;
- cin>>n>>v;
- FOR(i,,n)cin>>b[i];
- FOR(i,,n)cin>>a[i];
- mem(dp,);
- FOR(i,,n)For(j,v,)
- {
- if(a[i]<=j)dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
- }
- cout<<dp[v]<<endl;
- }
- return ;
- }
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 - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...
- [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 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- HDU 2602 Bone Collector 0/1背包
题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 2602 Bone Collector(经典01背包问题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/O ...
- hdu 2602 Bone Collector(01背包)模板
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...
随机推荐
- Python3 字符编码到底是个什么鬼
首先ASCII码是美国人自己给自己用的,只针对英文及一系列符号,凭想象预留了编码位置,不料有个东方大国文字过于复杂,预留根本不够,所以这个大国重新搞了个编码gb2312.gbk等,结果就是全世界各国都 ...
- 决解nginx代理的django项目的admin站点无法访问,和没样式的问题。
首先我们先解决无法访问admin站点的问题 首先我们先修改一下nginx的配置,添加红色框框的部分. 然后重新启动一下nginx 访问一下admin站点 发现没有样式了. 我们先修改/fast_foo ...
- Java基础语法-运算符
1算术运算符 1.1运算符和表达式 运算符:对常量和变量进行操作的符号. 表达式:用运算符把常量或者变量连接起来符合java语法的式子就可以称为表达式. 不同运算符链接的表达式体现的是不同类型的表达式 ...
- javascript中的继承-寄生组合式继承
前文说过,组合继承是javascript最常用的继承模式,不过,它也有自己的不足:组合继承无论在什么情况下,都会调用两次父类构造函数,一次是在创建子类原型的时候,另一次是在子类构造函数内部.子类最终会 ...
- linux-memcache安装及memcached memcache扩展
linux memcached安装yum -y install libevent libevent-deve yum list memcached yum -y install memcached m ...
- 使用 VS Code 搭建 TypeScript 开发环境
使用 VS Code 搭建 TypeScript 开发环境 TypeScript 是 JavaScript 的超集,TypeScript 只是增强了 JavaScript 而非改变了 JavaScri ...
- Axiso解决跨域访问
问题: 在项目中需要需要讲本地项目去请求一个URL接口获取数据 例如: 本地请求地址:http://127.0.0.1:19323/site/info.json 请求Url地址:http://www. ...
- docker配置Nginx
创建及进入docker容器$docker run -p 80 --name web01 -i -t ubuntu /bin/bash 创建html文件夹$mkdir -p /var/www/html ...
- 前端播放m3u8格式视频
一.前端播放m3u8格式视频 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta chars ...
- Java并发(具体实例)——几个例子
一步步优化页面渲染功能 本节将模拟一个简单的页面渲染功能,它的作用是将HTML页面绘 ...