POJ 2976 Dropping tests

01 分数规划

利用实数域下的二分答案来解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define eps 1e-5
using namespace std;
int T, n, k;
double a[1005], b[1005], l, r, mid, num[1005];
bool chk(double x) {
	for(int i = 1; i <= n; i++) num[i] = a[i] - x * b[i];
	sort(num + 1, num + n + 1);
	double ans = 0.0;
	for(int i = n; i > k; i--) ans += num[i];
	if(ans >= 0.0) return 1;
	else return 0;
}
int main() {
	while(1) {
		cin >> n >> k;
		if(!n && !k) break;
		l = 0.0; r = 0.0;
		for(int i = 1; i <= n; i++) {
			cin >> a[i];
			r += a[i];
		}
		for(int i = 1; i <= n; i++) cin >> b[i];
		mid = (l + r) * 0.5;
		while(r - l >= eps) {
			mid = (l + r) * 0.5;
			if(chk(mid)) {
				l = mid;
			}else r = mid;
		}
		printf("%.0f\n", mid * 100);
	}
	return 0;
}
posted @ 2018-03-25 20:04  Mr_Wolfram  阅读(135)  评论(0编辑  收藏  举报