POJ 2976 0-1分数规划入门

很久之前做了,最近做居然忘了


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
#define ll long long

double a[1005],b[1005];
double d[1005];
int main ()
{
    ll n,k;
    while(scanf("%lld%lld",&n,&k)!=EOF)
    {
        if(n==0 && k==0) break;
        for(int i=1;i<=n;++i)
            scanf("%lf",&a[i]);
        for(int i=1;i<=n;++i)
            scanf("%lf",&b[i]);
        double left=0.0,right=1.0,mid;
        while(right-left>0.000001)
        {
            mid=(left+right)/2;
            for(int i=1;i<=n;++i)
                d[i]=a[i]-mid*b[i];
            sort(d+1,d+1+n);
            double res=0.0;
            for(int i=k+1;i<=n;++i)
                res+=d[i];
            if(res>0.0)
                left=mid;
            else right=mid;
        }
        // round
        int ans=left*100+0.5000001;
        cout<<ans<<endl;
    }
    return 0;
}





你可能感兴趣的:(POJ 2976 0-1分数规划入门)