题意
给你N个物品,每个物品有一个价值Vaule和花费Cost,让你挑选N - K个,使得最大。
题解
大佬博客链接:01分数规划问题相关算法与题目讲解(二分法与Dinkelbach算法)
这个题实在序列上的01分数规划,直接按vaule - x*cost排序就好了。
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + 5;
const double eps = 1e-5;
int n, k, a[MAXN], b[MAXN];
double d[MAXN];
bool check(double x)
{
for (int i = 0; i < n; i++) d[i] = a[i] - x*b[i];
sort(d, d+n);
double ans = 0;
for (int i = n-1; i >= k; i--) ans += d[i];
return ans >= 0;
}
int main()
{
while(~scanf("%d%d", &n, &k) && (n+k))
{
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n; i++) scanf("%d", &b[i]);
double l = 0, r = 1;
while (r-l > eps)
{
double mid = (l+r)/2;
if (check(mid)) l = mid;
else r = mid;
}
printf("%.0f\n", l*100);
}
return 0;
}
/*
3 1
5 0 2
5 1 6
4 2
1 2 7 9
5 6 7 9
0 0
*/