POJ 2976-Dropping tests(01分数规划入门)
https://blog.csdn.net/Hhaile/article/details/8883652
这篇写得挺不错
这个就是入门的01分数规划,二分答案x,将ai-x*bi从大到小排序,去前n-k个,判断是否满足sigm(ai)-sigm(bi*x)>=0,满足 说明还有最优解即更新l=mid,否则更新r=mid;
#include <stdio.h> #include <string.h> #include<iostream> #include <math.h> #include <algorithm> #include<functional> using namespace std; typedef long long ll; #define eps 1e-6 const int N = 1e6 + 10; int n,k; int a[N],b[N]; double c[N]; bool check(double t) { double sum=0; for (int i = 1; i <= n; i++) c[i] = a[i] - t*b[i]; sort(c + 1, c + n + 1, greater<double>() ); for (int i = 1; i <= n - k; i++) sum += c[i]; return sum >= 0; } int main() { while (cin >> n >> k && (n || k)) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; 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("%.0lf\n", l*100); } return 0; }