FatMouse' Trade

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

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
 
Recommend
JGShining

解题思路:本题为典型贪心算法题目,由于物品可分割,不能用01背包做。
      先对给出的各个仓库的信息(豆子量,所需猫粮量)进行分析,可知仓库中豆子越多,所需猫粮越少,则越划得来,兑换该房间的豆子可以得到最大的豆子量。
      因此,先求出各个仓库的豆子量/所需猫粮量的比值(简称比值),比值大的应该考虑优先兑换。
      然后将所有仓库信息按照比值从大到小排序,得出各仓库的兑换先后顺序,存储在结构体数组food[]里面,准备兑换。
      然后按排序结果对相应仓库进行兑换,若当前所剩猫粮量不为0并且还有仓库未进行兑换,则继续兑换,
                (1)如果当前老鼠剩下的猫粮大于兑换当前仓库所有的豆子的所需的猫粮量,则兑换该仓库的所有豆子,豆子总量增加该仓库总豆子量的值,所剩猫粮总量减去兑换当前仓库所有豆子所需猫粮量;
                (2)如果当前老鼠剩下的猫粮小于兑换当前仓库所有的豆子的所需的猫粮量,则兑换该仓库的所有豆子*所剩猫粮/所需的猫粮量,豆子总量增加所有豆子*所剩猫粮/所需的猫粮量(注意精度,这里的值可能会产生小数),所剩猫粮总量置0;
       最后,按题目要求输出兑换所得豆子总量(保留3位小数)即可。

#include<stdio.h>
#include<algorithm>
using namespace std;
struct node
{
int j;
int f;
double bi;
}food[1005]; //兑换情况,豆子量,所需猫粮,豆/猫比
bool cmp(node a,node b) //排序,按比例从大到小排序
{
return a.bi>b.bi;
}
int main()
{
int m,n;
int i,j;
while(scanf("%d%d",&m,&n)&&(n!=-1||m!=-1))
{
for(i=0;i<n;i++)
{
scanf("%d%d",&food[i].j,&food[i].f);
food[i].bi=(double)food[i].j/food[i].f;
}
sort(food,food+n,cmp);
double sum=0;
i=0;
while(m&&i<n) //当猫粮还有,豆子没有兑换完时,继续兑换
{
if(m>=food[i].f) //若当前猫粮能兑换当前仓库所有豆子,则全部兑换
{
sum+=food[i].j;
m-=food[i].f;
}
else //若当前猫粮无法兑换当前仓库所有猫粮,则按比例兑换
{
sum+=(double)m/food[i].f*food[i].j; //注意精度哦
m=0; //猫粮用完了
}
i++; //下一个房间(已按豆/猫比排序)比例不大于已兑换房间,且不小于所有未兑换的房间
}
printf("%.3lf\n",sum);
}
return 0;
}

HDU1009 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(贪心)

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

  3. Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏

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

  4. 1009 FatMouse' Trade

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

  5. FatMouse' Trade

    /* problem: FatMouse' Trade this is greedy problem. firstly:we should calculate the average J[i]/F[i ...

  6. HDU 1009 FatMouse' Trade(贪心)

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

  7. FatMouse' Trade -HZNU寒假集训

    FatMouse' Trade FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wa ...

  8. FatMouse' Trade(杭电ACM---1009)

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

  9. 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 ...

随机推荐

  1. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  2. 剑指Offer:找出数组中出现次数超过一半的元素

    题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...

  3. 设计模式Day02

    1.生成器模式 生成器模式也称为建造者模式.生成器模式的意图在于将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示. 生成器模式的编程步骤: (1)定义一个产品类:  由于不在该类完 ...

  4. HDU 4393 Throw nails

    Throw nails Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. Peer to Peer File Sharing Through WCF

    http://www.codeproject.com/Articles/614028/Peer-to-Peer-File-Sharing-Through-WCF https://github.com/ ...

  6. 让你的.NET程序支持多语言

    辛辛苦苦做出来的软件,我们当然希望能让更多的人用,支持多语言是必须的.下面我将以Asp.net Web Form为例来介绍如何支持多语言.其他程序比如windows程序,过程都是大同小异的. 大概分以 ...

  7. 430flash的操作

    大概印象:430的flash好像有点像arm的flash,只不过是arm的flash要比430的大很多,而且430的flash不同于E2PROOM,这一点需要值得注意 MSP430flash的基本特点 ...

  8. 【Spark学习】Apache Spark调优

    Spark版本:1.1.0 本文系以开源中国社区的译文为基础,结合官方文档翻译修订而来,转载请注明以下链接: http://www.cnblogs.com/zhangningbo/p/4117981. ...

  9. rabbitMQ 笔记

    1. 端口 rabbitMQ  server 使用的端口是5672  ,   AMQP协议的端口 rabbitMQ  web 使用的端口是15672  ,   管理工具的端口 rabbitMQ  cl ...

  10. maven创建web项目

    上一次自己使用Maven还是在大三在学校做项目时.现在公司有个新项目,想重新使用一下maven,顺便记下一些步骤 1.安装maven 1.1 访问(http://maven.apache.org/), ...