欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

01分数规划

程序员文章站 2022-05-21 19:59:42
...

一、内容

01分数规划
01分数规划

二、代码

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 1e3 + 9;
const double eps = 1e-7;
int n, k;
double a[N], b[N], tmp[N];
double g(double v) {
 	for (int i = 0; i < n; ++i) {
        tmp[i] = a[i] - v * b[i];
    }   
    sort(tmp, tmp + n);
    double sum = 0;
    for (int i = k; i < n; ++i) {
        sum  += tmp[i];
    }
    return sum;
}
double cal() {
    double l = 0, r = 1e10;
    while (r - l > eps) {
        double mid = (l + r) / 2;
        if (g(mid) > 0) {
            l = mid;
        } else {
            r = mid;
        }
    } 
    return l;
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
    }
    printf("%.2lf\n", cal());
    return 0;
}