POJ 2976- Dropping tests -01分数规划

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


经典01分数规划裸题


题意:

n门功课,有该门课的得分和总分,求去掉k门课后平均绩点最高可以是多少?

根据01分数规划推导结论


有:

/*
 *  0-1 分数规划
 *      t1 * x1 + t2 * x2 + ... + tn * xn
 *  r = ---------------------------------
 *      c1 * x1 + c2 * x2 + ... + cn * xn
 *  给定t[1..n], c[1..n], 求x[1..n]使得sigma(xi)=k且r最大(小). 
 *  为了让r最大, 先设计子问题z(r) = (t1 * x1 + .. + tn * xn) - r * (c1 * xn + .. + cn * xn);
 *  假设r的最优值为R. 则有:
 *  z(r) < 0 当且仅当 r > R;
 *  z(r) = 0 当且仅当 r = R;
 *  z(r) > 0 当且仅当 r < R;
 *  于是可二分求R.
 */

由于z(r)满足单调性

因此本题,我们可以求得所有的d[i]=100*a[i]-b[i],排序后,尽量选最大的前n-k个,然后求和,对其二分逼近答案

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <vector>
using namespace std;
#define inf 2147483647
const int N = 1005;

double aa[N],bb[N],c[N];
int n,m;

double bin(double x)
{
    for (int i=1; i<=n; i++)
        c[i]=100.0*aa[i]-x*bb[i];
    sort(c+1,c+1+n);
    double sum=0;
    for (int i=n; i>m; i--)
        sum+=c[i];
    return sum;
}
int main()
{

    while(cin>>n>>m)
    {
        if (!n&&!m) break;
        for (int i=1; i<=n; i++)
            scanf("%lf",&aa[i]);
        for (int i=1; i<=n; i++)
            scanf("%lf",&bb[i]);

        double l=0,r=1e16;
        while(r-l>1e-8)
        {
            double mid=(l+r)/2;
            if (bin(mid)>0)
                l=mid;
            else r=mid;
        }

        printf("%lld\n", (long long )(r+0.5));
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值