Dropping tests POJ - 2976 二分思想

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/82661485
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>


using namespace std;

const int maxn=1111;
const double eps=1e-8;

double x;
int n,k;
struct node
{
    int a,b;
    bool operator <(const node &t)const
    {
        return  a-b*x<t.a-t.b*x;
    }
}t[maxn];

bool judge(double mid)
{
    x=mid;
    sort(t,t+n);
    double ta,tb;
    ta=0;
    tb=0;
    for(int i=k;i<n;i++)
    {
        ta+=t[i].a;
        tb+=t[i].b;
    }
    if(ta/tb>mid)
        return true;
    else
        return false;
}

int main()
{
    while(1)
    {
        cin>>n>>k;
        if(n==0&&k==0)
            break;
        for(int i=0;i<n;i++)
            scanf("%d",&t[i].a);
        for(int i=0;i<n;i++)
            scanf("%d",&t[i].b);
        double l=0;
        double r=1.0;
        while(fabs(l-r)>eps)
        {
            double mid=(l+r)/2;
            if(judge(mid))
            {
                l=mid;
            }
            else
                r=mid;
        }
        printf("%.0lf\n",100*l);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/82661485
今日推荐