一、内容

01分数规划_ios
01分数规划_C_02

二、代码

#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;
}