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
 
解题心得:
  这是一个背包问题,但是还没学会动态规划的方法,只好用贪心法求解,但提交一直是wronganswer,谁看到了请告诉我。
 
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std; typedef struct{
int val;
int vol;
float vv;
}Bone; bool compare(Bone a,Bone b)
{
if(a.vv==b.vv){
return a.vol>b.vol;
}
return a.vv>b.vv;
} int main()
{
int t;//t组测试数据
int n;//n个骨头
int v;//书包能装的体积
int now_v=;//已装入的体积
int j1=;//已装入的个数
int sum=;//已装入的总价值
Bone bone[];
cin>>t;
for(int i=;i<t;i++){
scanf("%d %d",&n,&v);
for(int j=;j<n;j++){
scanf("%d",&bone[j].val);
}
for(int j=;j<n;j++){
scanf("%d",&bone[j].vol);
}
for(int j=;j<n;j++){
bone[j].vv=(float)bone[j].val/(float)bone[j].vol;
}
sort(bone,bone+n,compare);
//for(int j=0;j<n;j++){
// cout<<bone[j].vv<<" ";
// cout<<bone[j].val<<" "<<endl;
//}
for(int j=;j<n;j++){
now_v+=bone[j].vol;//循环一次往里装一次
j1++;
if(now_v>=v){
break;
}
}
if(now_v==v){
for(int j=;j<j1;j++){
sum+=bone[j].val;
}
}
if(now_v>v){
for(int j=;j<j1-;j++){
sum+=bone[j].val;
}
}
if(now_v<v){
for(int j=;j<n;j++){
sum+=bone[j].val;
}
}
cout<<sum<<endl;
now_v=;
sum=;
j1=;
}
return ;
}
 
 

HDU 2602 Bone Collector WA谁来帮忙找找错的更多相关文章

  1. HDU 2602 Bone Collector 0/1背包

    题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  2. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  3. hdu 2602 Bone Collector(01背包)模板

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...

  4. HDU 2602 Bone Collector

    http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  5. HDU 2602 Bone Collector(经典01背包问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/O ...

  6. HDU 2602 Bone Collector (简单01背包)

    Bone Collector http://acm.hdu.edu.cn/showproblem.php?pid=2602 Problem Description Many years ago , i ...

  7. hdu 2602 Bone Collector 背包入门题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 题目分析:0-1背包  注意dp数组的清空, 二维转化为一维后的公式变化 /*Bone Coll ...

  8. HDU 2602 Bone Collector(01背包裸题)

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

随机推荐

  1. str_replace vs preg_replace

    转自:http://benchmarks.ro/2011/02/str_replace-vs-preg_replace/ 事实证明str_replace确实比preg_replace快. If you ...

  2. 关于在windows上的wamp集成环境和xampp上安装mongo扩展

    今天来学习下mongodb,在装PHP扩展的时候本来是一个很轻松的事情,结果并不是我想想的那么简单. 我的集成环境是xampp的php版本是5.6的x86.我开启了安全模式,所以我需要mongo时ts ...

  3. PHP: 手把手编写自己的 MVC 框架实例教程

    1 什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...

  4. 希望各位博友能对我的自我介绍提出意见(要面试IBM的应用开发工程师,本科应届生一枚)

    面试官你好,首先我非常高兴能参加今天的面试. 我叫XXX(我名字里面有光宗耀祖),也许父母希望我光宗耀祖吧,所以给我起这样的名字.我的家乡山西太原,本科就读于XX大学,专业是信息与计算科学. 我今天要 ...

  5. flask 知识点总结

    ============================request对象的常用属性============================具体使用方法如下:request.headers, requ ...

  6. 密码学初级教程(六)数字签名 Digital Signature

    密码学家工具箱中的6个重要的工具: 对称密码 公钥密码 单向散列函数 消息认证码 数字签名 伪随机数生成器 提问: 有了消息认证码为什么还要有数字签名? 因为消息认证码无法防止否认.消息认证码可以识别 ...

  7. 1_mysql +DBA职业发展

    MYSQL + DBA 职业发展 mysql :the world's most popular open source database 最流行的开源数据库 数据库世界 关系数据库(又称SQL数据库 ...

  8. QT连接Linux mysql注意

    windows: #define MYSQLDB "QMYSQL"#define MYSQLDB_HOSTNAME "192.168.228.168"#defi ...

  9. 利用PHP读取文件

    $fp=fopen("D:\\phpStudy\\www\\date\\file\\2.txt","r");if($fp){    while(!feof($f ...

  10. Ubuntu 14 如何打开 .chm格式文档?

    好多手册是.chm格式,Ubuntu是需要安装第三方软件才能打开.chm格式文档,操作方式如下: 到“软件中心” -> 搜索“xchm”,并安装 -> 右键某个.chm文档,选择“属性” ...