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. linux系统命令:yum和apt-get

    转:http://my.oschina.net/liuyuanyuangogo/blog/546850 1.linux系统分类 一般来说著名的linux系统基本上分两大类: RedHat系列:Redh ...

  2. 包介绍 - UriTemplates (用于处理格式化Uri模板)

    UriTemplates 用于处理格式化Uri模板 PM> Install-Package Tavis.UriTemplates 设置Uri Path Segment [Fact] public ...

  3. HDU 2010

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int Is_SXH(int num); int main() { int in1, ...

  4. HDOJ 1576 A/B

    数论水题... A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. mysql explain 中key_len的计算

    今天丁原问我mysql执行计划中的key_len是怎么计算得到的,当时还没有注意,在高性能的那本书讲到过这个值的计算,但是自己看执行计划的时候一直都没有太在意这个值,更不用说深讨这个值的计算了: ke ...

  6. Lvs原理及部署之ARP协议

    1.什么使ARP协议 ARP协议,全称"Address Resolution Protocol" ,中文名是地址解析协议,使用ARP协议可实现通过IP地址获得对应的物理地址(MAC ...

  7. JQuery的Ajax跨域请求的解决方案

    客户端调用代码示例: var myurl = "http://js.yingdoo.com/embed/CAPTCHA.ashx?m=" + phone_val + "& ...

  8. 全栈工程师学习Linux技术的忠告

    随着科技的普及,Linux作为最受欢迎的服务端操作系统,无人不知,无人不晓.当今,不论是服务器搭建,还是客户端开发,Linux系统的基础技能对全栈来说都是必备的,而了解如下几个问题可以更好的帮助你成为 ...

  9. leetcode 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  10. python 函数的调用 和执行 小知识

    1.符号表 执行一个函数会引入一个用于函数的局部变量的新符号表. 更确切地说, 函数中的所有的赋值都是将值存储在局部符号表: 而变量引用首先查找局部符号表, 然后是上层函数的局部符号表, 然后是全局符 ...