NYOj 976 Youth的最大化

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,k;
double s[10005],w[10005],v[10005];
bool Greedy(double x)
{
    for(int i = 0;i < n;i++)
        s[i] = v[i] - x * w[i];
    sort(s,s+n);
    double sum = 0;
    for(int i = n-k;i < n;i++)
        sum += s[i];
    if(sum >= 0) return true;
    return false;
}
void search(double l,double r)
{
    while(r - l > 0.0000001)
    {
        double mid = (l + r) / 2;
        if(Greedy(mid)) l = mid;
        else r = mid;
    }
    printf("%.2lf\n",l);
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        double max = 0;
        for(int i = 0;i < n;i++)
        {
            scanf("%lf%lf",&w[i],&v[i]);
            if(max < v[i]/w[i])
                max = v[i]/w[i];
        }
        search(0,max);
    }
}



这个是关键



你可能感兴趣的:(贪心,二分)