题目大意:给定n个分数的分子x和分母y,定义average=sigma(x)/sigma(y),先要将n个分数去掉k个,问average最大是多少;
题目解析:二分答案ans,ok判断的时候对x-y*ans进行sort然后在累加得出答案;
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
double a,b;
}q[1010];
int n,k;
double ans;
bool cmp(node x,node y)
{
return x.a-ans*x.b>y.a-ans*y.b;
}
bool ok(double t)
{
double a=0,b=0;
ans=t;
sort(q+1,q+1+n,cmp);
for(int i=1;i<=n-k;i++)
{
a+=q[i].a;
b+=q[i].b;
}
return a/b>t;
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF&&(n||k))
{
for(int i=1;i<=n;i++)
scanf("%lf",&q[i].a);
for(int i=1;i<=n;i++)
scanf("%lf",&q[i].b);
double l=0,r=2;
while(abs(r-l)>=1e-5)
{
double mid=(l+r)/2;
if(ok(mid))
{
l=mid;
}
else
{
r=mid;
}
}
printf("%d\n",(int)(l*100+0.5));
}
return 0;
}