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. java.lang.Class

    java.lang.Class 一.Class类是什么 Class是一个类,位于java.lang包下. 在Java中每个类都有一个相对应的Class类的对象,换句话说:Java程序在启动运行时 一个 ...

  2. MessageFormat用法

    MessageFormat用来格式化一个消息,通常是一个字符串,比如: String str = "I'm not a {0}, age is {1,number,short}", ...

  3. jQuery源码-jQuery.fn.attr与jQuery.fn.prop

    jQuery.fn.attr.jQuery.fn.prop的区别 假设页面有下面这么个标签,$('#ddd').attr('nick').$('#ddd').prop('nick')分别会取得什么值? ...

  4. MySQL性能优化的最佳经验,随时补充

    1.为查询优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一个缓存 ...

  5. AlwaysOn可用性组功能测试(一)--AlwaysOn故障转移测试

    具体测试环境请参考: AlwaysOn可用性组测试环境安装与配置(一)--SQL群集环境搭建 AlwaysOn可用性组测试环境安装与配置(二)--AlwaysOn配置(界面与T-SQL) 一. Alw ...

  6. 在Mac mini上编译Android源码

    参考文章 1.Android 6.0 源代码编译实践 2.编译Android源码致命错误解决方案 实践过程 1.Mac下安装Ubuntu双系统 (1)Ubuntu版本:Ubuntu 15.10 注:实 ...

  7. Android 实现简单音乐播放器(二)

    在Android 实现简单音乐播放器(一)中,我介绍了MusicPlayer的页面设计. 现在,我简单总结一些功能实现过程中的要点和有趣的细节,结合MainActivity.java代码进行说明(写出 ...

  8. git之remote branch controller(远程分支控制)

    1.创建本地分支 git branch  //查看远程分支 git checkout -b branch_name //创建远程分支 在查看分支git branch 2.将分支提交到远程仓库 此时远程 ...

  9. 分享一个c#t的网页抓取类

    using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Ne ...

  10. CSS浏览器兼容性与解析问题终极归纳

    1.怪异模式问题:漏写DTD声明,Firefox仍然会按照标准模式来解析网页,但在IE中会触发怪异模式.为避免怪异模式给我们带来不必要的麻烦,最好养成书写DTD声明的好习惯. 2.IE6双边距问题:在 ...