传送门:

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. spring_boot启动报错

    配置好pom文件后,在controller加注解,如下: 运行后报错!!! 发现配置加的是多此一举,修改为下边的,运行OK

  2. Windbg 脚本命令简介 一

    Windbg  脚本命令简介 一 Windbg command r: registers的简写,可以显示或修改寄存器的值.浮点寄存器的值.定义别名变量. 可以显示当前线程下的寄存器值. The r c ...

  3. javaweb之EL自定义函数

    1.什么是EL自定义函数 EL自定义函数是在EL表达式中调用的某个java类的静态方法,这个静态方法需在web应用程序中进行配置才可以被EL表达式调用.EL自定义函数可以扩展EL表达式的功能,让EL表 ...

  4. python6

    集合-set    集合是高中数据中的一个概念.    确定的一堆无需数据,集合中的买个数据称为一个集合       集合的定义         1.创建空集合             变量 = se ...

  5. java 文件的上传和下载

    主要介绍使用 smartupload.jar 包中的方法对文件的上传和下载.上传时文件是存放在服务器中,我用的是tamcat. 首先建立一个servlet 类,对文件的操作 package com.d ...

  6. CentOS 7 禁用IPV6以提高网速

    方法 1 编辑文件/etc/sysctl.conf,$vi /etc/sysctl.conf添加下面的行: net.ipv6.conf.all.disable_ipv6 = net.ipv6.conf ...

  7. arcMap 进度条

    private void kk() { IProgressDialogFactory progressDialogFactory = new ProgressDialogFactoryClass(); ...

  8. Jmeter对HTTP请求压力测试、并发测试

    最近公司需要开发一个简单的报名系统,供外网用户提供报名服务,由于我们公司是个初创的微型公司,开发人员都是刚毕业不久,开发经验相当缺乏. 对于服务器性能测试这块的经验更是少得可以忽略.迫使不得不让我们去 ...

  9. 属性只有一个值的这类 html 属性是怎么回事,该如何设置值;比如:checked = “checked” vs checked = true

    参考链接:https://stackoverflow.com/questions/10650233/checked-checked-vs-checked-true 问: What is the dif ...

  10. MongoDB安装步骤

    安装C:\Users\Administrator>d:\mongo\bin\mongod -dbpath=D:\ND.Monodb\ND.Monodb.db -logpath=D:\ND.Mon ...