牛客暑期多校第五场A:gpa题解(简单01分数规划)

链接:https://www.nowcoder.com/acm/contest/143/A
来源:牛客网
 

题目描述

Kanade selected n courses in the university. The academic credit of the i-th course is s[i] and the score of the i-th course is c[i].

At the university where she attended, the final score of her is 

Now she can delete at most k courses and she want to know what the highest final score that can get.

输入描述:

The first line has two positive integers n,k

The second line has n positive integers s[i]

The third line has n positive integers c[i]

输出描述:

Output the highest final score, your answer is correct if and only if the absolute error with the standard answer is no more than 10-5

示例1

输入

复制

3 1
1 2 3
3 2 1

输出

复制

2.33333333333

说明

Delete the third course and the final score is 

备注:

1≤ n≤ 105

0≤ k < n

1≤ s[i],c[i] ≤ 103

这道题是简单01分数规划题,直接套模版,不用考虑超时的情况。

不了解01分数规划的童鞋(也叫最大平均数问题)看这里

01分数规划解析

与这道题类似的基础01分数规划题

更优的Dinkelbach算法,不过一般题目用二分法做就ok

ac代码:

#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define maxn 100010
#define eps 1e-8//精度设的更高一点当然没错啦
using namespace std;
int main()
{
    double s[maxn],c[maxn],a[maxn];
    int n,k,i,j;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
        scanf("%lf",&s[i]);
    for(i=0;i<n;i++)
        scanf("%lf",&c[i]);
    double l=0,r=1000;
    while((r-l)>eps)
    {
        double mid=(r+l)/2,maxf=0;
        for(i=0;i<n;i++)
            a[i]=s[i]*c[i]-mid*s[i];
        sort(a,a+n);
        for(i=n-1;i>=k;i--)
            maxf+=a[i];
        if(maxf>=0) l=mid;
        else r=mid;
    }
    printf("%.11f\n",r);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Cassie_zkq/article/details/81478856