• hdu-1009~(贪心)


    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

    FatMouse准备了M磅的猫食,准备与守卫仓库的猫交易,这些猫包含他最喜欢的食物,JavaBean。
    仓库有N个房间。第i间房间包含J [I]磅的JavaBeans,并且需要F [i]磅的猫粮。 FatMouse不必交易房间内的所有JavaBeans,相反,如果他付给F [i] * 1磅的猫粮,他可能会得到1磅的JavaBeans。这里是一个实数。现在他正在为你分配这个作业:告诉他他可以获得的最大JavaBeans数量。


    输入

    输入由多个测试用例组成。每个测试用例都以包含两个非负整数M和N的行开始。然后N行包含两个非负整数J [i]和F [i]。最后一个测试用例后面跟着两个-1。所有整数不大于1000。


    产量

    对于每个测试用例,在一行中打印一个真实数字,精确到3位小数,这是FatMouse可以获得的最大JavaBean数量。


    示例输入

    5 3
    7 2
    4 3
    5 2
    20 3
    25 18
    24 15
    15 10
    -1 -1


    示例输出

    13.333
    31.500

    解题思路:贪心思想,算出单价排序,然后从大到小去取.看代码

    #include <iostream>
    #include <math.h>
    #include <stdio.h>
    #include <algorithm>
    
    using namespace std;
    struct stu
    {
        double ja,mao,dj;
    } c[1000];
    bool cmp(stu x,stu y)
    {
        return x.dj>y.dj;
    }
    int main()
    {
        int m,n;
        while(scanf("%d %d",&m,&n),m!=-1&&n!=-1)
        {
            double a[5000],b[5000],ans=0;
            for(int i=1; i<=n; i++)
            {
                scanf("%lf %lf",&c[i].ja,&c[i].mao);
                c[i].dj=c[i].ja/c[i].mao;
            }
            sort(c+1,c+n+1,cmp);
           for(int i=1;i<=n;i++)
            {
                if(m>=c[i].mao)
                {
                    m-=c[i].mao;
                    ans+=c[i].ja;
                }
                else
                {
                    ans+=m*c[i].dj;
                    break;
                }
            }
            printf("%.3lf
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    新增数据盘
    FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。
    将tomcat的protocol改为APR模式,以提高性能
    POJ 2195 Going Home (最小费用最大流)
    HDU 2732 Leapin' Lizards (最大流)
    POJ 2516 Minimum Cost (最小费用最大流)
    POJ 1087 A Plug for UNIX (最大流)
    POJ 3281 Dining (最大流)
    HDU 3605 Escape (最大流)
    POJ 1149 PIGS (最大流)
  • 原文地址:https://www.cnblogs.com/mashen/p/8530568.html
Copyright © 2020-2023  润新知