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
ZJCPC2004

Recommend
JGShining

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

#include<iostream>  
#include<algorithm>
#include<iomanip>
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(cin>>m>>n&&(n!=-1||m!=-1))
    {
        for(i=0;i<n;i++)
        {
            cin>>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++;  //下一个房间(已按豆/猫比排序)比例不大于已兑换房间,且不小于所有未兑换的房间  
        }
        cout<<fixed<<setprecision(3)<<sum<<endl;
    }
    return 0;
}

杭电1009-FatMouse' Trade的更多相关文章

  1. 杭电 1009 FatMouse' Trade (贪心)

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

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

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

  3. HDU 1009 FatMouse' Trade(简单贪心 物品可分割的背包问题)

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

  4. FatMouse&#39; Trade(杭电1009)

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

  5. HDOJ.1009 FatMouse' Trade (贪心)

    FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...

  6. Hdu 1009 FatMouse' Trade

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

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

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

  8. 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) ...

  9. 1009 FatMouse' Trade

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

  10. HDU 1009 FatMouse' Trade(贪心)

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

随机推荐

  1. Fusioncharts使用说明

    背景 最近由于工作需要,再次接触到了Fusioncharts,但也有不足之处,现在官网上似乎是不支持flash的版本了,只能看到html5相关的javascript版本,无奈再次从网上搜索到了一些别人 ...

  2. Codeforce Round #218 Div2

    A:没个元素的个数少的变成多的和就是了 B:居然被systemtest搓掉了- -分东西,我改的代码,还是shit一样的过的...别人的直接两个操作数相减就可以了! C:二分题- -,没想到比赛时因为 ...

  3. Codeforce Round #210 Div2

    A:对角线为k其他为0 B:利用两个相邻的数一定gcd为1和1与任何数gcd为1错k个位就行了 C:不会做操蛋,好像是因为上一层的始终小于下一层的 好吧C又研究了一下,是个贪心题,不符合的情况先科不考 ...

  4. How to export a template in Visual Studio?

    Create a customize template file: 1.template arguments introduction like: 上图只是其中一部分,更多请查看文后的参考资源 tem ...

  5. c# 隐藏 控制台应用程序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. 王家林 大数据Spark超经典视频链接全集[转]

    压缩过的大数据Spark蘑菇云行动前置课程视频百度云分享链接 链接:http://pan.baidu.com/s/1cFqjQu SCALA专辑 Scala深入浅出经典视频 链接:http://pan ...

  7. C#Windows窗体应用程序MyKTV项目

    后台管理其中有一个添加歌手信息和歌曲信息的窗体要点击按钮并上传文件,因为对那些文件流什么的不懂,所以用了老师教的最简单的判断方法,但此方法只是按后缀名判断文件的样式,如果后缀名乱改就不行了! 此时需要 ...

  8. WebForm中<%=%>与<%#%>的区别?

    1<%=%>相当于Response.Write(),是输出变量的值 2<%#%>专门用于数据绑定,可以绑定一些变量或者数据源中的信息,中间绑定是数据源的条目,若想让它起作用,必 ...

  9. paper 61:计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接

    转载出处:blog.csdn.net/carson2005 以下链接是本人整理的关于计算机视觉(ComputerVision, CV)相关领域的网站链接,其中有CV牛人的主页,CV研究小组的主页,CV ...

  10. mongo自带测试

    benchrun > res = benchRun({ ops:[{ ns:"test.foo", op:"insert", doc:{y:{,]}} } ...