poj 2976(分数规划)

poj 2976
分数规划简单题,具体看这里

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
const int MAXN = 1000 + 5;
int n, k, ai[MAXN], bi[MAXN];
double p[MAXN];
bool check(double x) {
    for (int i = 1; i <= n; i++) p[i] = (db)ai[i] - (db)bi[i] * x;
    sort(p + 1, p + 1 + n);
    double tot = 0;
    for (int i = k + 1; i <= n; i++) tot += p[i];
    return tot >= 0;
}
void clean() {}
void solve() {
    clean();
    for (int i = 1; i <= n; i++) scanf("%d", &ai[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &bi[i]);
    double l = 0, r = 1;
    for (int i = 1; i <= 100; i++) {
        double mid = (l + r) / 2.0;
        if (check(mid)) l = mid; else r = mid;
    }
    printf("%.0f\n", l * 100);
}
int main() {
    while (scanf("%d%d", &n, &k) == 2 && (n || k)) solve();
    return 0;
}
------ 本文结束 ------