acdream 1032 put on make up(二分01 分数规划)

题目链接:http://www.acdream.net/problem.php?id=1032

题意:给出n组数据(ai,bi),在其中选取k组,使得ln(a1*a2*…*ak)/ln(b1*b2*…*bk)最大?

思路:二分答案。





struct node

{

    double a,b;

};



const int MAX=10005;

node p[MAX];

int n,m;

double mid;



int cmp(node a,node b)

{

    return a.a-a.b*mid>b.a-b.b*mid;

}



int main()

{

    while(scanf("%d%d",&n,&m)!=-1)

    {

        int i,t;

        for(i=1;i<=n;i++)

        {

            scanf("%d",&t);

            p[i].a=log(1.0*t);

        }

        for(i=1;i<=n;i++)

        {

            scanf("%d",&t);

            p[i].b=log(1.0*t);

        }

        double low=0,high=1e10,x,y;

        while(high-low>1e-10)

        {

            mid=(low+high)/2;

            sort(p+1,p+n+1,cmp);

            x=y=0;

            for(i=1;i<=m;i++)

            {

                x+=p[i].a;

                y+=p[i].b;

            }

            if(x/y-mid>1e-10) low=mid+1e-10;

            else high=mid-1e-10;

        }

        printf("%.3lf\n",mid);

    }

    return 0;

}

  

你可能感兴趣的:(Make)