Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This

Bone Collector

HDU 2602

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 ?

Input

The 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.

Output

One integer per line representing the maximum of the total value (this number will be less than 231).

多年前,在泰迪的家乡,有一个叫“骨头收藏家”的人。这个人喜欢收集各种各样的骨头,比如狗的,牛的,他还去了坟墓……
骨头收集者有一个V容量的大袋子,在他的收集之旅中,有很多骨头,显然,不同的骨头有不同的价值和不同的体积,现在给定每块骨头在他旅行中的价值,你能计算出骨头收集器可以得到的最大值吗?
输入

第一行包含一个整数 T ,即案例数。
接下来是T个案例,每个案例三行,第一行包含两个整数N,V,(N <= 1000 , V <= 1000 )代表骨头的数量和他的包的体积。第二行包含 N 个整数,代表每个骨骼的值。第三行包含 N 个整数,代表每个骨骼的体积。

输出

每行一个整数,代表总值的最大值(该数字将小于 2 31)。

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14

题意描述:给你骨头的数量和骨头收集器的体积,以及每个骨头的值和体积,求骨头收集器可以装的骨头的值的最大值

解题思路:01背包问题,可以利用二维数组或者一维数组来写

一维数组AC

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main(void)
{int t;scanf("%d",&t);while(t--){ int n,v;//种类,包体积int dp[1050],p[1050],q[1050];memset(dp,0,sizeof(dp));scanf("%d %d",&n,&v);for(int i=1;i<=n;i++)scanf("%d",&p[i]);//价值 for(int i=1;i<=n;i++)scanf("%d",&q[i]);//体积 for(int i=1;i<=n;i++)for(int j=v;j>=q[i];j--)dp[j]=max(dp[j],dp[j-q[i]]+p[i]);printf("%d\n",dp[v]);}return 0;
}

二维数组AC

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[1050][1050];
struct Bone
{int v;//骨头的体积,int w;//价值
}bone[1050];
int main(void)
{int t,n,d;scanf("%d",&t);while(t--){memset(dp,0,sizeof(dp));scanf("%d %d",&n,&d);//骨头的数量。包的体积 for(int i=1;i<=n;i++)scanf("%d",&bone[i].w);for(int i=1;i<=n;i++)scanf("%d",&bone[i].v);for(int i=1;i<=n;i++){for(int j=0;j<=d;j++){if(bone[i].v>j)//装不下的时候dp[i][j]=dp[i-1][j];elsedp[i][j]=max(dp[i-1][j],dp[i-1][j-bone[i].v]+bone[i].w); }}printf("%d\n",dp[n][d]);} return 0;}

Bone Collector(01背包问题-两种写法)相关推荐

  1. HDU Bone Collector (01背包问题)

    之前背包问题学了忘学了忘= =是太笨了. 题目 许多年前,在泰迪的家乡,有一个人被称为"骨收集者".这个人喜欢收集各种骨头,例如狗,牛的骨头,他也去了坟墓-- 骨头收集者有一个大袋 ...

  2. 两种写法的效果一样,那么到底哪一种更好呢?

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 有时候,我们在写一些循环逻辑的时候,并不是按执行次数等作 ...

  3. Model层的两种写法

    Model层的两种写法 第一种写法 namespace MyMVC.Models {public class Child{ //属性private int id;public int Id{get { ...

  4. controller 有两种写法,讨论一下两种写法的区别:

    controller 有两种写法,讨论一下两种写法的区别: 写法 1: app.controller('myCtrl', function($scope, $location) { $scope.my ...

  5. Sql语句中 case when .. 的两种写法

    在 SQL查询语句中, case 语句的两种写法(SqlServer 2005 下测试通过): 1. select (case 字段1  when a then 0  when b then 1  e ...

  6. 20210408:力扣(二分查找法的两种写法以及变体题目)

    二分查找法的两种写法以及变体题目 写在前面 题目 思路与算法 代码实现 写在最后 写在前面 关于二分查找,真的是一个非常实用的查找算法,主要有两种写法,今天在总结时再次碰到,再次整理,方便后续查看复习 ...

  7. python装饰器带参数函数_python带参数装饰器的两种写法

    python带参数装饰器的两种写法 前言 最近在实现一个装饰器的过程中发现了一个很有意思的地方,在博客里面分享出来 不同的写法 三层函数嵌套,实现了可传参数的一个装饰器. import logging ...

  8. sum 去重_总结leetcode上【排列问题】【组合问题】【子集问题】回溯算法去重的两种写法!...

    本周小结!(回溯算法系列三)续集 在 本周小结!(回溯算法系列三) 中一位录友对 整颗树的本层和同一节点的本层有疑问,也让我重新思考了一下,发现这里确实有问题,所以专门写一篇来纠正,感谢录友们的积极交 ...

  9. Vue2基础-el与data的两种写法(HTML版)

    目录 一.el的2种写法 二.data的2种写法 三. 一个重要的原则 Vue2基础全套教程合集:点击跳转        Vue2高级全套教程合集:点击跳转 一.el的2种写法 new Vue时候配置 ...

最新文章

  1. java fork_浅谈Java的Fork/Join并发框架
  2. php 删除xls文件,使用PHPExcel将xls文件转换为xlsx时出错
  3. nginx http子模块conf的初始化
  4. lightoj 1031 区间dp
  5. 在VC中动态加载ODBC的方法
  6. 除了 Tensorflow、PyTorch ,还有哪些深度学习框架值得期待?
  7. JavaScript函数之实际参数对象(arguments) / callee属性 / caller属性 / 递归调用 / 获取函数名称的方法...
  8. leetcode题库1314-- 矩阵区域和
  9. 修改java的jre_applet访问本地资源,需要修改jre的java.policy
  10. git_day03_01——git远程仓库的使用
  11. 一大波干货学习资源分享
  12. Java简易电影院系统
  13. 微信小程序数据懒加载
  14. 北京PMP考试考点在哪里?
  15. numpy 矩阵求和小技巧
  16. tit-al00 android 6,华为TIT-AL00入网 MTK6735四核全网通手机
  17. 一元购抽奖号码 thinkphp php
  18. CDN 的功能有哪些?
  19. 我收集的优秀Flash站点
  20. logit regression

热门文章

  1. 计算机桌面下方标图,电脑下方怎么设置显示桌面图标
  2. 推特由于技术问题,我们无法完成此次请求,请重试
  3. 华视电子vue身份证读取
  4. 深入理解Java虚拟机——运行时栈帧结构(局部变量表)
  5. java 红绿灯_java -- GUI 红绿灯
  6. x265-10bit的配置
  7. python股票查询系统_使用python获取股票的上市日期等基本信息
  8. (一)极大似然估计法原理讲解
  9. 什么蓝牙耳机适合打游戏?打游戏不延迟的蓝牙耳机推荐
  10. Arduino ESP32 WIFI 蓝牙模式触控按键切换