[POJ 2976]Dropping tests(0-1分数规划)

Description

In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be.

Given your test scores and a positive integer k, determine how high you can make your cumulative average if you are allowed to drop any k of your test scores.

Suppose you take 3 tests with scores of 5/5, 0/1, and 2/6. Without dropping any tests, your cumulative average is . However, if you drop the third test, your cumulative average becomes .

Solution

0-1分数规划裸题

设能够达到的最大值为p

则有∑(ai*xi)/∑(bi*xi)<=p

∑ai*xi-∑bi*xi*p<=0

即∑xi(ai-bi*p)<=0

也就是说∑xi(ai-bi*p)的最大值为0

二分答案

如果p<mid ∑xi*(ai-bi*mid)的最大值<0

如果p>mid ∑xi*(ai-bi*mid)的最大值>0

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define eps 1e-10
using namespace std;
int n,k;
double a[1005],b[1005],c[1005];
int main()
{
    while(~scanf("%d %d",&n,&k)&&n)
    {
        for(int i=1;i<=n;i++)scanf("%lf",&a[i]);
        for(int i=1;i<=n;i++)scanf("%lf",&b[i]);
        double l=0,r=1,mid;
        while(r-l>eps)
        {
            mid=(l+r)/2;
            for(int i=1;i<=n;i++)c[i]=a[i]-b[i]*mid;
            sort(c+1,c+1+n);
            double res=0;
            for(int i=k+1;i<=n;i++)
            res+=c[i];
            if(res>0)l=mid;else r=mid;
        }
        printf("%.0f
",mid*100);
        
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Zars19/p/6942372.html