HDUOJ--Bone Collector
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21463 Accepted Submission(s): 8633
Problem Description
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 ?
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).
Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1
Sample Output
14
Author
Teddy
Source
http://acm.hdu.edu.cn/showproblem.php?pid=2602
Recommend
lcy
背包问题.....一定要多练习...
代码:----
1 #include<stdio.h>
2 #include<string.h>
3 #define maxn 1005
4 int dp[maxn],arr[maxn][2];
5 int max(int a,int b)
6 {
7 return a>b?a:b;
8 }
9
10 void zeroonepack(int cost ,int value,int v)
11 {
12 for(int i=v;i>=cost;i--)
13 dp[i]=max(dp[i],dp[i-cost]+value);
14
15 }
16 int main()
17 {
18 int t,n,v ,i;
19 scanf("%d",&t);
20 while(t--)
21 {
22 scanf("%d%d",&n,&v);
23 memset(dp,0,sizeof dp);
24 for(i=0;i<n;i++)
25 {
26 scanf("%d",arr[i]+0);
27 }
28 for(i=0;i<n;i++)
29 {
30 scanf("%d",arr[i]+1);
31 }
32 for(i=0;i<n;i++)
33 zeroonepack(arr[i][1],arr[i][0],v);
34 printf("%dn",dp[v]);
35 }
36 return 0;
37 }
优化后代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct st
{
int a;
int b;
};
typedef struct st sta;
int main()
{
int test,n,v,i,j;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&v);
int *dp =(int *)malloc(sizeof(int)*(v+1));
sta *stu =(sta *)malloc(sizeof(sta)*(n+1));
for(i=0;i<n;i++)
scanf("%d",&stu[i].a);
for(i=0;i<n;i++)
scanf("%d",&stu[i].b);
for(i=0;i<=v;i++)
dp[i]=0;
for(i=0;i<n;i++)
{
for(j=v ; j>=stu[i].b ; j--)
{
if(dp[j]<dp[j-stu[i].b]+stu[i].a)
dp[j]=dp[j-stu[i].b]+stu[i].a;
}
}
printf("%dn",dp[v]);
free(dp);
free(stu);
}
return 0;
}
- 分享Mac/Linux系统Shell终端利器SecureCRT以及注册破解方法
- MySQL中GTID和自增列的数据测试(r12笔记第38天)
- 分享一个入门级可控多线程shell脚本方案
- PhalGo-介绍
- 实测Nginx服务器开启pagespeed加速效果
- MySQL频繁停库的问题分析(r12笔记第33天)
- 解决Nginx Helper插件一键清理缓存功能导致网站打不开问题
- Oracle 12c DBCA浅析(r12笔记第48天)
- MySQL中的derived table(r12笔记第47天)
- Oracle数据库端口突然无法访问的分析(r12笔记第46天)
- MySQL中的反连接(r12笔记第45天)
- Nginx配置多站点下的Proxy_cache或Fastcgi_cache缓存加速
- MySQL主从不一致的细小问题分析(r12笔记第62天)
- Linux在批量服务器管理中实用的PS1命令提示符格式
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- XSS Game分析以及知识点总结
- Web 图形可视化 SQL 优化神奇,真香!
- C++ vector 容器浅析
- C++中的STL中map用法详解
- C++ pair(对组)的简单了解
- elasticSearch学习(六)
- centos7搭建LDAP服务器
- zookeeper is not a recognized option zookeeper参数不支持
- 快来看看你是不是“假的”DBA
- 基于docker搭建gitlab
- 理解Future及FutureTask的实现
- centos7 hbase1.4.13+hadoop2.7.1+单机环境搭建
- 关于kubeconfig多集群切换
- 代码版本管理规范
- 基于云原生系统的应用分发系统设计