牛客网暑期ACM多校训练营(第五场). gpa(0 / 1 分数规划)

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

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld

题目描述

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

解题思路:0 / 1 分数规划

二分答案,假设当前二分了一个值D,我们要判断是否存在一个方案使得总绩点>=D,找到之后把前 k 个删除就好了。时间复杂度:O(nlogn) 。

参考代码:

#include<bits/stdc++.h>
using namespace std;
long long n,k,i,j;
long long s[100050],c[100050];
double pj[100050];
bool pd(double x)
{
    for(i=0;i<n;i++) pj[i]=s[i]*c[i]-x*s[i];
    sort(pj,pj+n);
    double sum=0;
    for(i=k;i<n;i++) sum+=pj[i];
    return sum>0;
}

int main()
{
    cin>>n>>k;
    for(i=0;i<n;i++) cin>>s[i];
    for(i=0;i<n;i++) cin>>c[i];
    
    double l=0,r=2000000,mid;
    for(j=0;j<50;j++)
    {
        mid=(l+r)/2;
        if(pd(mid)) l=mid;
        else r=mid;
    }
    printf("%.11lf\n",r);
}

猜你喜欢

转载自blog.csdn.net/XxxxxM1/article/details/81389746