POJ2976 Dropping tests 01规划+二分

第一次接触这种类型的题。。贴上博客:https://blog.csdn.net/mosquito_zm/article/details/78585094

说实话想清楚了并不难,我还是太菜了,二分到现在都写得不好QAQ…

附上AC代码吧:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;

const int MAX=1005;
int n,k;
int score[MAX];//分数
int num[MAX];//学分
double wei[MAX];
bool solve(double mid)//注意是double
{
    memset(wei,0.0,sizeof(wei));
    for(int i=0;i<n;i++)
        wei[i]=1.0*score[i]-mid*num[i];
    sort(wei,wei+n);
    double ans=0;
    for(int i=n-1;i>=k;i--)
        ans+=wei[i];
    if(ans<=0)//mid偏大
        return true;
    else
        return false;
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        if(n==0&&k==0)
            break;
        for(int i=0;i<n;i++)
            scanf("%d",&score[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&num[i]);
        double l=0.0,r=1.0;
        while(r-l>(1e-6))//注意!!写成"="循环结束不了
        {
            double mid=(l+r)/2.0;
            if(solve(mid))
                r=mid;//不能+1,因为double
            else
                l=mid;
        }
        l*=100.0;
        printf("%.0f\n",l);
    }
    return 0;
}

 

点赞