01分数规划
原创
©著作权归作者所有:来自51CTO博客作者tizzi的原创作品,请联系作者获取转载授权,否则将追究法律责任
一、内容
二、代码
#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;
}