[POJ2976]Dropping tests(01分数规划)

版权声明:转载请注明出处:http://blog.csdn.net/clove_unique https://blog.csdn.net/Clove_unique/article/details/55250489

题目描述

传送门
题意:给出ai,bi,选择至少n-k个,使100*aibi最大

题解

01分数规划裸题…
化一下式子R=aibi,那么当di=aiRbi>0的时候一定存在更优解
二分R,算出di,贪心地选前n-k大,然后判断是否存在更优解就可以了

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 1005

const double eps=1e-4;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
int n,k;
double a[N],b[N],d[N],ans;

bool check(double mid)
{
    for (int i=1;i<=n;++i) d[i]=a[i]-mid*b[i];
    sort(d+1,d+n+1);
    double now=0;
    for (int i=n;i>k;--i)
        now+=d[i];
    return dcmp(now)>=0;
}
double find()
{
    double l=0.0,r=1.0,mid,ans;
    while (dcmp(r-l)>0)
    {
        mid=(l+r)/2.0;
        if (check(mid)) ans=l=mid;
        else r=mid;
    }
    return ans;
}
int main()
{
    while (~scanf("%d%d",&n,&k))
    {
        if (!n&&!k) break;
        for (int i=1;i<=n;++i) scanf("%lf",&a[i]);
        for (int i=1;i<=n;++i) scanf("%lf",&b[i]);
        ans=100.0*find();
        printf("%.0lf\n",ans);
    }
}
展开阅读全文

没有更多推荐了,返回首页