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
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 ?
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.
背包问题.....一定要多练习...
代码:----
#include<stdio.h>
#include<string.h>
#define maxn 1005
int dp[maxn],arr[maxn][];
int max(int a,int b)
{
return a>b?a:b;
} void zeroonepack(int cost ,int value,int v)
{
for(int i=v;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+value); }
int main()
{
int t,n,v ,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&v);
memset(dp,,sizeof dp);
for(i=;i<n;i++)
{
scanf("%d",arr[i]+);
}
for(i=;i<n;i++)
{
scanf("%d",arr[i]+);
}
for(i=;i<n;i++)
zeroonepack(arr[i][],arr[i][],v);
printf("%d\n",dp[v]);
}
return ;
}
优化后代码:
#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+));
sta *stu =(sta *)malloc(sizeof(sta)*(n+));
for(i=;i<n;i++)
scanf("%d",&stu[i].a);
for(i=;i<n;i++)
scanf("%d",&stu[i].b);
for(i=;i<=v;i++)
dp[i]=; for(i=;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("%d\n",dp[v]);
free(dp);
free(stu);
}
return ;
}
HDUOJ--Bone Collector的更多相关文章
- hdu 2602 Bone Collector(01背包)模板
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 2602 Bone Collector WA谁来帮忙找找错
Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collec ...
- Bone Collector(01背包)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/N 题目: Description Many year ...
- HDU 3639 Bone Collector II(01背包第K优解)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Bone Collector II
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 2602 Bone Collector (简单01背包)
Bone Collector http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , i ...
- hdu 2602 Bone Collector 背包入门题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目分析:0-1背包 注意dp数组的清空, 二维转化为一维后的公式变化 /*Bone Coll ...
- hdu 2639 Bone Collector II
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 杭电 2602 Bone Collector
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 2602 Bone Collector
http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- linux统计文件夹内文件数
for dir in `find ./ -type d ` ;do echo -n "$dir " ;find $dir -type f | wc -l ;echo " ...
- strcat实现
//将源字符串加const,表明其为输入参数 char*strcat(char*strDest,constchar*strSrc) { //后文returnaddress,故不能放在assert断言之 ...
- Go 语言简介(上)— 语法
周末天气不好,只能宅在家里,于是就顺便看了一下Go语言,觉得比较有意思,所以写篇文章介绍一下.我想写一篇你可以在乘坐地铁或公交车上下班时就可以初步了解一门语言的文章.所以,下面的文章主要是以代码和注释 ...
- C语言编译器不检查数组下标越界
这两天被人问了一个问题说假如C/C++访问下表越界的数组元素会报错么,于是充满好奇心的我动手试了一下,WTF,果然没有报错,但是会给程序带来莫名其妙的结果(比如十次的循环但是变成了死循环,但八次却可以 ...
- 笔记本建立wifi热点的实用详细步骤
准备工作: (1) 首先要打开开始菜单--->搜索“services.msc” 并打开(或者用win+R快捷键打开“运行”输入“service.msc”,点击确定)--->找到“WLAN ...
- 【Nodejs】理想论坛帖子下载爬虫1.04
一直想做一个能把理想论坛指定页范围的帖子都能完整下载下来的爬虫,但未能如愿. 主要的障碍在并发数的控制和长时间任务的突然退出,比如想下载前五页的帖子,分析后可得到大约15000个主贴或子贴,如果用回调 ...
- PHP高级教程-安全邮件
PHP Secure E-mails 在上一节中的 PHP e-mail 脚本中,存在着一个漏洞. PHP E-mail 注入 首先,请看上一章中的 PHP 代码: <html> < ...
- Java从零开始学二(标识符和关键字)
标识符.关键字.注释 一.标识符 Java中的包.类.方法.参数和变量的名字由任意顺序的大小字母.数字.下划线(_).和美元符号($)组成, 标识符:不能以数字开头.也不能是JAVA中的保留关键字 如 ...
- axios 请求参数配置说明
axios的配置项地址参考: https://www.npmjs.com/package/axios { // `url` is the server URL that will be used fo ...
- vim中的高亮全部同样单词的方法
用vim时,想高亮显示一个单词并查找的方发.将光标移动到所找单词. 1: shift + "*" 向下查找并高亮显示 2: shift + "#" 向上查找 ...