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 ...
随机推荐
- IT技术 | 让程序员抓狂的排序算法教学视频
点击「箭头所指处」可快速关注传智特刊微信号:CZTEKAN 原文地址:http://mp.weixin.qq.com/s?__biz=MjM5OTM4NDMyMg==&mid=20056820 ...
- (转载博文)VC++API速查
窗口处理 2.1 窗口简介 2.2.1 创建普通窗口(CreateWindow.CreateWindowEx) 2.2.2 关闭窗口(CloseWindow) 2.2.3 销毁窗口(DestroyWi ...
- UML_行为图
活动图是UML用于对系统的动态行为建模的另一种常用工具,它描述活动的顺序,展现从一个活动到另一个活动的控制流.活动图在本质上是一种流程图.活动图着重表现从一个活动到另一个活动的控制流,是内部处理驱动的 ...
- #include <stack>
1 pop(); 出栈 2 push(); 入栈 3 size(); 返回栈中元素个数 4 top(); 返回栈顶元素 使用栈,把十进制转换为二进制 #include <iostream> ...
- #include <list>
clear();删除向量中的所有对象 erase(iterator it);删除it所指向的容器对象 insert(iterator it,const T&);向it所指的向量位置前插入一个对 ...
- 浅析C# 中object sender与EventArgs e (转)
一.了解C#中的预定义事件处理机制 在写代码前我们先来熟悉.net框架中和事件有关的类和委托,了解C#中预定义事件的处理. EventArgs是包含事件数据的类的基类,用于传递事件的细节. Ev ...
- 连不上VSS 【转】
今天打开项目,但是连不上VSS,报错如下: (一)现象: Could not find the Visual SourceSafe Internet Web Service connection in ...
- 编译hadoop2.2.0源码时报错
编译hadoop2.2.0源码时, mvn install -DskipTests 报错: [ERROR] COMPILATION ERROR : [INFO] ------------------- ...
- hibou 主界面自己侧滑的定义
要打滑View参加UIPanGestureRecognizer #pragma mark 手势识别器回调方法 - (void)dragView:(UIPanGestureRecognizer *)ge ...
- <!--转换office时需要此配置 --> <identity impersonate="true" />
1.需要对Office 进行操作时 ,添加权限 <!--转换office时需要此配置 --> <identity impersonate="true" /> ...