2018牛客多校第五场A.gpa (二分)

标题:A、gpa | 时间限制:1 秒 | 内存限制:256M 
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≤n≤10^5

0≤ k<n

1≤s[i],c[i] ≤10^3 
 
示例 1

输入

3 1

1 2 3

3 2 1

输出 

2.33333333333

题目大意:给出每门的课的学分与绩点,算总绩点,输出误差在10-5。

思路:比赛时死活过不了,全是wa,心态爆炸...我的方法确实是错的,不然应该是超时而不是wa。之前的想法是把学分除以绩点再排序,再找去除1-k课的最大情况。然而是错的,联想以前看过的文章,\sum (a*b)/\sum c!=\sum (a*b)/c,应该是死在这儿了,,,

正确的做法:

所以二分找最大绩点就行了........(出题人说需要注意二分的上界不然会超时)心好累好累好累。

代码如下:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
long long n,k,i,j;
long long s[100050],c[100050];
double p[100050];
bool fun(double x)
{
    for(i=0;i<n;i++)
    {
        p[i]=s[i]*c[i]-x*s[i];
    }
    sort(p,p+n);
    double sum=0;
    for(i=k;i<n;i++)
    {
        sum+=p[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=5000000,mid;
    for(j=0;j<80;j++)
    {
        mid=(l+r)/2;//二分找最大学分 
        if(fun(mid)) l=mid;
        else r=mid;
    }
    printf("%.11lf\n",r);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/PleasantlY1/article/details/81382016