POJ 2976 Dropping tests | 01分数规划

题目:

http://poj.org/problem?id=2976


题解:

枚举答案

如果最大的n-k个a[i]-mid*b[i]之和大于等于0就OK否则不OK

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 1005
using namespace std;
int n,k;
double a[N],b[N],pos[N];
bool check(double mid)
{
    for (int i=1;i<=n;i++) pos[i]=a[i]-mid*b[i];
    sort(pos+1,pos+n+1);
    double res=0;
    for (int i=n;i>=k+1;i--)
    res+=pos[i];
    return res>=0.0;
}
int main()
{
    while (scanf("%d%d",&n,&k)!=EOF)
    {
    for (int i=1;i<=n;i++) scanf("%lf",&a[i]);
    for (int i=1;i<=n;i++) scanf("%lf",&b[i]);
    if (n+k==0) break;
    double l=0.0,r=1.0;
    for (int i=0;i<100;i++)
    {
        double mid=(l+r)/2.0;
        if (check(mid)) l=mid;
        else r=mid;
    }
    printf("%d\n",(int)(l*100+0.5));
    }
    return 0;
}

 

posted @ 2018-01-09 20:06  MSPqwq  阅读(107)  评论(0编辑  收藏  举报