Bone Collector(01背包+记忆化搜索)
Bone Collector
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 21 Accepted Submission(s) : 6
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 2[sup]31[/sup]).
5 10
1 2 3 4 5
5 4 3 2 1
#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
int f[];
int val[];
int cos[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(f,,sizeof(f));
memset(val,,sizeof(val));
memset(cos,,sizeof(cos));
int n,v;
scanf("%d%d",&n,&v);
int i,j;
for(i=;i<=n;i++)
scanf("%d",&val[i]);
for(j=;j<=n;j++)
scanf("%d",&cos[j]);
for(i=;i<=n;i++)
{
for(j=v;j>=cos[i];j--)
{
// f[j]=f[j-1];
// if(j>=cos[i])
f[j]=max(f[j],f[j-cos[i]]+val[i]);
}
}
printf("%d\n",f[v]);
}
return ;
}
记忆化搜索;
ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=;
int dp[MAXN][MAXN],w[MAXN],p[MAXN];
int dfs(int i,int v){
if(dp[i][v])return dp[i][v];
if(i==||v<)return ;//
if(w[i]>v)dp[i][v]=dfs(i-,v);
else dp[i][v]=max(dfs(i-,v),dfs(i-,v-w[i])+p[i]);//
return dp[i][v];
}
int main(){
int T,N,M;
SI(T);
while(T--){
SI(N);SI(M);
for(int i=;i<=N;i++)SI(p[i]);
for(int i=;i<=N;i++)SI(w[i]);
mem(dp,);
printf("%d\n",dfs(N,M));
}
return ;
}
Bone Collector(01背包+记忆化搜索)的更多相关文章
- 01背包-记忆化搜索到成型的DP
记忆化搜索 #include<bits/stdc++.h> using namespace std; typedef long long ll; int n,W; int dp[105][ ...
- HDU 2602 Bone Collector(01背包裸题)
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu2602 Bone Collector 01背包
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like ...
- HDU 2602 - Bone Collector - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...
- [原]hdu2602 Bone Collector (01背包)
本文出自:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //=================================== ...
- hdu2602 Bone Collector (01背包)
本文来源于:http://blog.csdn.net/svitter 题意:典型到不能再典型的01背包.给了我一遍AC的快感. //================================== ...
- HDU 2602 Bone Collector --01背包
这种01背包的裸题,本来是不想写解题报告的.但是鉴于还没写过背包的解题报告.于是来一发. 这个真的是裸的01背包. 代码: #include <iostream> #include < ...
- ACM HDU Bone Collector 01背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 这是做的第一道01背包的题目.题目的大意是有n个物品,体积为v的背包.不断的放入物品,当然物品有 ...
- 解题报告:hdu2602 Bone collector 01背包模板
2017-09-03 15:42:20 writer:pprp 01背包裸题,直接用一维阵列的做法就可以了 /* @theme: 01 背包问题 - 一维阵列 hdu 2602 @writer:ppr ...
随机推荐
- 检测android的网络链接状态
http://www.oschina.net/question/100267_61129?sort=default&p=1#tags_nav http://www.cnblogs.com/to ...
- Activity Window View的关系
http://blog.csdn.net/chiuan/article/details/7062215 http://blog.163.com/fenglang_2006/blog/static/13 ...
- linux swap 分区调控(swap分区 lvm管理)
注:linux swap分区 采用lvm管理,调控可以采用下面的方法 一.查看 swap lv [root@testdb ~]# vgdisplay -v Finding all volume ...
- Unix/Linux环境C编程入门教程(21) 各个系统HelloWorld跑起来效果如何?
Unix/Linux家族人员众多,我们无法一一讲解如何配置环境. 本文选定我们在前面安装的RHEL6 RHEL7 MAC10.9.3 Solaris11如何跑起来helloworld RHEL 6 上 ...
- Javascript base64加密 解密
var base64encodechars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ...
- C编译器、链接器、加载器详解
摘自http://blog.csdn.net/zzxian/article/details/16820035 C编译器.链接器.加载器详解 一.概述 C语言的编译链接过程要把我们编写的一个c程序(源代 ...
- #include <amp.h>
parallel_for_each(av.extent, [=](concurrency::index<1>idx)restrict(amp) {av[idx] += 1; }); //[ ...
- Leetcode:best_time_to_buy_and_sell_stock_II题解
一.题目 如果你有一个数组,它的第i个元素是一个股票在一天的价格. 设计一个算法,找出最大的利润. 二.分析 假设当前值高于买入值,那么就卖出,同一时候买入今天的股票,并获利.假设当前值低于买入值,那 ...
- 谈谈 css 的各种居中——读编写高质量代码有感
css 的居中有水平居中和垂直居中,这两种居中又分为行内元素居中和块级元素居中,不同的居中用不同方法. 水平居中 1.行内元素水平居中(文本,图片) 给父层设置 text-align:center; ...
- 关于jQuery的ajax的源码的dataType解读
$.ajax其实底层还是用的XMLHttpRequest,对于加载数据的格式datatype有:xml.text.html.json.jsonp.script. 其中xml.text不需要处理,直接使 ...