传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1009

FatMouse' Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 92493    Accepted Submission(s): 32082

Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
 
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
 
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
 
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
 
Sample Output
13.333
31.500
 
Author
CHEN, Yue
 
Source
 分析:
贪心问题,一个类似物品可分割的背包问题
思想:
先按照物品单位价值排序(物品总价值/物品总重量)
每次选择单位价值最大的
选到选择某个物品不能选这个物品的全部的时候就选择物品的一部分(可分割嘛)
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max_v 1005
struct node
{
double v,c;
}p[max_v];
bool cmp(node a,node b)
{
return (a.v/a.c)>(b.v/b.c);
}
int main()
{
int m,n;
while(~scanf("%d %d",&m,&n))
{
if(n==-&&m==-)
break;
for(int i=;i<n;i++)
{
scanf("%lf %lf",&p[i].v,&p[i].c);
}
sort(p,p+n,cmp);
double sum=;
for(int i=;i<n;i++)
{
if(m>=p[i].c)
{
sum+=p[i].v;
m-=p[i].c;
}else
{
sum+=(m*(p[i].v/p[i].c));
break;
}
}
printf("%0.3lf\n",sum);
}
return ;
}

HDU 1009 FatMouse' Trade(简单贪心 物品可分割的背包问题)的更多相关文章

  1. hdu 1009:FatMouse' Trade(贪心)

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. HDU 1009 FatMouse' Trade【贪心】

    解题思路:一只老鼠共有m的猫粮,给出n个房间,每一间房间可以用f[i]的猫粮换取w[i]的豆,问老鼠最多能够获得豆的数量 sum 即每一间房间的豆的单价为v[i]=f[i]/w[i],要想买到最多的豆 ...

  3. [题解]hdu 1009 FatMouse' Trade(贪心基础题)

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  4. HDU 1009 FatMouse' Trade (贪心)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1009 题目大意:肥鼠准备了 磅的猫粮,准备和看管仓库的猫交易,仓库里装有他最喜爱的食物 豆.仓库有 个 ...

  5. HDU 1009 FatMouse' Trade (贪心算法)

    题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换, ...

  6. HDU 1009 FatMouse' Trade(简单贪心)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  7. HDU 1009 FatMouse' Trade(贪心)

    FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...

  8. Hdu 1009 FatMouse' Trade 2016-05-05 23:02 86人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  9. Hdu 1009 FatMouse' Trade

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. UVA1339(字母映射)

    memcmp(const void *buf1, const void *buf2, unsigned int count)可以比较两个串相等 http://baike.baidu.com/link? ...

  2. jquery not() 方法

    1.not(expression) 根据表达式参数的值,从包装集里删除元素 example : $('img[alt]').not('[alt*=joy]') 返回包含属性alt的img元素,但img ...

  3. javascript判断一个元素是另外一个元素的子元素

    javascript判断一个元素是另外一个元素的子元素用途有很多,最常用的就是当点击页面的空白处去执行某些操作,比如弹出层等. function isParent (obj,parentObj){ w ...

  4. MySQL--localhost、局域网、外网访问MySQL

    安装环境: MySQL版本:mysql-installer-community-5.7.22.1.msi,64位 计算机:Windows7旗舰版,64位 本机的局域网IP为:192.168.2.230 ...

  5. Raspberry U盘操作

    项目系统要求的对U盘分区,分出系统盘与用户盘.这就有了今天的这个总结了: 1.输入命令“fdisk -l”查看设备挂载的位置,因为这个在设备挂载的时候有可能会发生变化. 假设设备挂载到了 /dev/s ...

  6. Java常用开发组件介绍

    1.Lombok的使用 http://www.cnblogs.com/ywqbj/p/5711691.html 2.Guava的使用 http://www.yiibai.com/guava/

  7. idea 出现 java.noSuchMechodFound

    公司 用了多个项目来相互之间形成依赖.每次修改或者添加新功能,会升级版本.用的是maven,这几天 一直 出现一个问题就是:本地 升级版本完后 使用 git命令 mvn -deploy -e 打包后, ...

  8. spring文章

    单元测试 spring +Junit 完美组合:https://blog.csdn.net/shan9liang/article/details/40452469#

  9. 使用redis存放 map数据

    效果 实现 public class JedisPoolUtil { /** * 获取一个redis实例 * @param jedisConnectionFactory * @return */ pu ...

  10. Python 冒泡排序法分析

    冒泡排序法 def maopao(): array = [2,1,3,6,5,4] #确定一组需要排序的数值列表 for i in range(len(array)-1): #大循环次数=列表长度,但 ...