zoukankan      html  css  js  c++  java
  • hdu—1009贪心算法

    第一道贪心算法 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1009

    #include <stdio.h>
    #include <iostream>
    #include <algorithm>
    #include <stdlib.h>
    using namespace std;
    struct node
    {
        double x,y;
        double rate;
    }f[100000];
    int cmp(node a,node b)
    {
        return a.rate>b.rate;
    
    }
    int main()
    {
        int  n,m;
        int i,k;
        //int j[1005],f[1005];
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            if(n==-1&&m==-1)break;
            for(i=0;i<m;i++)
                {
                    scanf("%lf%lf",&f[i].x,&f[i].y);
                    f[i].rate=f[i].x/f[i].y;
                }
            sort(f,f+m,cmp);
            double left=n,sum=0;
            for(i=0;i<m&&left>0;i++)
            {
                if(left>=f[i].y)
                {
                    sum+=f[i].x;
                    left-=f[i].y;
                }
                else
                    {
                        sum+=left*(f[i].x/f[i].y);
                        break;
                }
            }
            printf("%.3lf
    ",sum);
        }
    return 0;
    
    }
  • 相关阅读:
    最大流问题的几种经典解法综述
    有上下界的网络流
    hiho一下
    poj 1018
    状压dp
    hdu 1043
    Poj1015
    7.14
    sgu 128
    (zhuan)
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3383000.html
Copyright © 2011-2022 走看看