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

本文介绍了一种使用0-1分数规划算法来解决如何通过丢弃特定数量的测试成绩以提高累计平均分的问题。该算法首先设定能达到的最大值p,并通过二分查找法确定最优解。文中提供了一个具体的示例,展示了当允许丢弃某些测试成绩时,如何显著提高累计平均分。

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\n",mid*100);
        
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Zars19/p/6942372.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值