题意:有一个式子是
给出n组a[i]和b[i],去掉k组,让剩下的a[i]和b[i]带入式子计算出的最大值是多少。
题解:和poj3111的思路类似,只不过这个要四舍五入,注意强制转换。
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 1005;
struct P {
int a, b;
double x;
}p[N];
int n, k;
bool cmp(P a, P b) {
return a.x > b.x;
}
bool judge(double s) {
for (int i = 0; i < n; i++)
p[i].x = p[i].a * 1.0 - s * p[i].b;
sort(p, p + n, cmp);
double temp = 0;
for (int i = 0; i < k; i++)
temp += p[i].x;
return temp >= 0;
}
int main() {
while (scanf("%d%d", &n, &k) == 2 && n + k) {
k = n - k;
double l = 0, r = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &p[i].a);
r += p[i].a;
}
for (int i = 0; i < n; i++)
scanf("%d", &p[i].b);
for (int i = 0; i < 100; i++) {
double mid = (l + r) / 2;
if (judge(mid))
l = mid;
else
r = mid;
}
printf("%d\n", (int)(100 * (l + 0.005)));
}
return 0;
}