题目大意:
有
n
n
n个地方,去到第
i
i
i个地方会有
a
i
a_i
ai和
b
i
b_i
bi
要去
k
k
k个地方,怎么选择使得
∑
a
i
/
∑
b
i
(
i
ϵ
S
)
\sum a_i/\sum b_i(i\ \epsilon \ S)
∑ai/∑bi(i ϵ S)
S表示去的地方的集合
解题思路:
然后我们二分K
然后每次取
a
i
/
k
−
b
i
ai/k−bi
ai/k−bi最大的
k
k
k个就好了
Accepted code:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100005;
int n, k;
double l, r, a[N], b[N], c[N];
double read() {
int flag = 1, f = 0; char c = getchar();
while (!isdigit(c)) { if (c == '-') flag = -1; c = getchar();}
while (isdigit(c)) f = f * 10 + c - 48, c = getchar();
return (double)f * flag;
}
bool Check(double checknum) {
double ans = 0;
for (int i = 1; i <= n; i++)
c[i] = a[i] / checknum - b[i];
sort(c+1, c+1+n);
for (int i = n; i >= n - k + 1; i--)
ans += c[i];
return ans >= 0;
}
int main() {
n = (int)read(), k = (int)read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= n; i++) b[i] = read();
l = 1e-4, r = 20000;
while (r - l > 1e-4) {
double mid = (l + r) / 2.0;
if (Check(mid)) l = mid;
else r = mid;
}
printf("%0.3lf", l);
}