这个题算是01分数规划的最基本的应用了, 01分数规划是给你n对数(a1, b1)....(an, bn), 然后让你选择一些数对, 使得sigma(ai)/sigma(bi)最大。这里附上讲解一份, http://blog.csdn.net/hhaile/article/details/8883652, 代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> using namespace std; typedef long long LL; const int maxn = 1000 + 10; const double eps = 1e-4; int n, k; double ai[maxn], bi[maxn]; double ci[maxn]; double check(double mid) { for(int i=0; i<n; i++) ci[i] = ai[i]*100 - mid*bi[i]; sort(ci, ci+n); double res = 0.0; for(int i=k; i<n; i++) res += ci[i]; return res; } int main() { while(cin>>n>>k) { if(n==0 && k==0) break; for(int i=0; i<n; i++) scanf("%lf", &ai[i]); for(int i=0; i<n; i++) scanf("%lf", &bi[i]); double l=0.0, r=100; double ans; while(r-l > eps) { double mid = (l+r)/2; if(check(mid) >= -eps) l=ans=mid; else r=mid; } printf("%.0f\n", ans); } return 0; }
时间: 2024-10-11 21:50:07