• 关于【贪心算法】


    FatMouse

    题目描述:

    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. 

    输入:

    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.

    输出:

    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.

    样例输入:
    5 3
    7 2
    4 3
    5 2
    20 3
    25 18
    24 15
    15 10
    -1 -1
    样例输出:
    13.333
    31.500

    Code:
    #include <cstdio>
    #include <algorithm>
     
    using namespace std;
     
    struct goods{
        double weight;        //该物品总重
        double totalValue;    //该物品总价值
        double price;         //该物品性价比
    };
     
    bool cmp(goods a,goods b){
        return a.price>=b.price;
    }
     
    int main()
    {
        double n;   //一共多少钱
        const int arrSize=1010;
        int m;      //有几样物品
        goods arr[arrSize];
        while(scanf("%lf%d",&n,&m)!=EOF){
            if(n==-1&&m==-1)
                break;
            for(int cnt=0;cnt<m;++cnt){
                scanf("%lf%lf",&arr[cnt].weight,&arr[cnt].totalValue);
                arr[cnt].price=arr[cnt].weight/arr[cnt].totalValue;
            }
            sort(arr,arr+m,cmp);
            double ans=0;
            int index=0;
            while(n>0&&index<m){
                if(n>arr[index].totalValue){   //若能买下全部该商品
                    ans+=arr[index].weight;
                    n-=arr[index].totalValue;
                }else{
                    ans+=n/arr[index].totalValue*arr[index].weight;
                    n=0;
                }
                ++index;
            }
            printf("%.3lf
    ",ans);
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1433
        User: lcyvino
        Language: C++
        Result: Accepted
        Time:10 ms
        Memory:1020 kb
    ****************************************************************/

    今年暑假不AC

    题目描述:

    “今年暑假不AC?”“是的。”“那你干什么呢?”“看世界杯呀,笨蛋!”“@#$%^&*%...”确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

    输入:

    输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

    输出:

    对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

    样例输入:
    12
    1 3
    3 4
    0 7
    3 8
    15 19
    15 20
    10 15
    8 18
    6 12
    5 10
    4 14
    2 9
    0
    样例输出:
    5

    Code:
    #include <cstdio>
    #include <algorithm>
     
    using namespace std;
     
    struct program{
        int startTime;
        int endTime;
    };
     
    bool cmp(program a,program b){
        return a.endTime<=b.endTime;
    }
     
    int main()
    {
        const int arrSize=110;
        program arr[arrSize];
        int n;
        while(scanf("%d",&n)!=EOF){
            if(n==0)
                break;
            for(int cnt=0;cnt<n;++cnt){
                scanf("%d%d",&arr[cnt].startTime,&arr[cnt].endTime);
            }
            sort(arr,arr+n,cmp);
            int currentTime=0;
            int ans=0;
            for(int index=0;index<n;++index){
                if(currentTime<=arr[index].startTime){
                    currentTime=arr[index].endTime;
                    ++ans;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1434
        User: lcyvino
        Language: C++
        Result: Accepted
        Time:10 ms
        Memory:1020 kb
    ****************************************************************/

    迷瘴

    题目描述:

    通过悬崖的yifenfei,又面临着幽谷的考验——
    幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体内,便会全身溃烂而死。
    幸好yifenfei早有防备,提前备好了解药材料(各种浓度的万能药水)。现在只需按照配置成不同比例的浓度。
    现已知yifenfei随身携带有n种浓度的万能药水,体积V都相同,浓度则分别为Pi%。并且知道,针对当时幽谷的瘴气情况,只需选择部分或者全部的万能药水,然后配置出浓度不大于 W%的药水即可解毒。
    现在的问题是:如何配置此药,能得到最大体积的当前可用的解药呢?
    特别说明:由于幽谷内设备的限制,只允许把一种已有的药全部混入另一种之中(即:不能出现对一种药只取它的一部分这样的操作)。

    输入:

    输入数据的第一行是一个整数C,表示测试数据的组数;
    每组测试数据包含2行,首先一行给出三个正整数n,V,W(1<=n,V,W<=100);
    接着一行是n个整数,表示n种药水的浓度Pi%(1<=Pi<=100)。

    输出:

    对于每组测试数据,请输出一个整数和一个浮点数;
    其中整数表示解药的最大体积,浮点数表示解药的浓度(四舍五入保留2位小数);
    如果不能配出满足要求的的解药,则请输出0 0.00。

    样例输入:
    3
    1 100 10
    100
    2 100 24
    20 30
    3 100 24
    20 20 30
    样例输出:
    0 0.00
    100 0.20
    300 0.23

    Code:
    #include <cstdio>
    #include <algorithm>
     
    using namespace std;
     
    int main()
    {
        int C;
        int n,V,W;
        const int arrSize=100;
        int arr[arrSize];
        while(scanf("%d",&C)!=EOF){
            for(int cnt=0;cnt<C;++cnt){
                scanf("%d%d%d",&n,&V,&W);
                for(int j=0;j<n;++j)
                    scanf("%d",&arr[j]);
                sort(arr,arr+n);
                int current_V=0;
                double current_W=0;
                for(int k=0;k<n;++k){
                    if(((current_W*current_V+arr[k]*V)/(current_V+V))<=W){
                        current_W=(current_W*current_V+arr[k]*V)/(current_V+V);
                        current_V+=V;
                    }
                }
                printf("%d %.2lf
    ",current_V,current_W/100);
            }
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1435
        User: lcyvino
        Language: C++
        Result: Accepted
        Time:0 ms
        Memory:1020 kb
    ****************************************************************/
  • 相关阅读:
    转:什么是即时编译(JIT)!?OpenJDK HotSpot VM剖析
    用好spring mvc validator可以简化代码
    接口服务中的日志
    rest api参数与content-type
    【单页应用】全局控制器app应该干些什么?
    【webapp的优化整理】要做移动前端优化的朋友进来看看吧
    【单页应用】理解MVC
    【单页应用】view与model相关梳理
    【单页应用之通信机制】view之间应该如何通信
    【单页应用巨坑之History】细数History带给单页应用的噩梦
  • 原文地址:https://www.cnblogs.com/Murcielago/p/4188271.html
Copyright © 2020-2023  润新知