2018 Niuke 多校 第5场 A gpa 二分+整数规划

题目描述 

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

题意


题解


代码

/**
 *  题意 : 最大化T = sum(s[i]*c[i]) / sum(s[i]) 
 *  允许删除 k 个 s[i] , c[i] 最大化上式
 *  根据二分的思想 我们可以二分 D 不断使得 T >= D 成立
 *  那么 T>=D 变成 sum(s[i]*c[i]) >= sum(s[i]) * D
 *  sum(s[i]*c[i]) - sum(s[i])*D >= 0 成立即可
 *  只需要排序后将 前k个小的删除后 判断上式是否成立即可 因为前k个有可能为负数
 */
#include<bits/stdc++.h>
#define exp 1e-8
using namespace std;

const int maxn = 1e5+10;
int s[maxn],c[maxn],n,k;
double res[maxn];
bool check(double D) {
    for(int i=0;i<n;i++) res[i] = s[i] * (c[i] - D);
    sort(res,res+n);
    double ans = 0;
    for(int i=k;i<n;i++) ans += res[i];
    return ans >= 0;
}
int main()
{
    while(~scanf("%d%d",&n,&k)) 
    {
        for(int i=0;i<n;i++) scanf("%d",&s[i]);
        for(int i=0;i<n;i++) scanf("%d",&c[i]);
        double l = 0,r = 1000;
        while(r - l > exp) {
            double mid = (r + l) * 0.5;
            if(check(mid)) l = mid;
            else r = mid;
        }
        printf("%.6f\n",r);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38013346/article/details/81367746