[ 2018牛客网暑期ACM多校训练营(第五场)A题] gpa

本文介绍了一道关于从n门课程中选择删除k门以获得最高总绩点的问题,并给出了01分数规划的解决方案。通过设定目标值x进行二分查找,找到使总绩点最大的x值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
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 s[i]c[i]s[i]∑s[i]c[i]∑s[i]
Now she can delete at most k courses and she want to know what the highest final score that can
get.

Input
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
Output the highest final score, your answer is correct if and only if the absolute error with the
standard answer is no more than 10510−5.

Note
1≤ n≤ 105
0≤ k < n
1≤ s[i],c[i] ≤ 103

Sample Input
3 1
1 2 3
3 2 1
Sample Output
2.33333333333
Hint
Delete the third course and the final score is 22+312+1=732∗2+3∗12+1=73.


题目大意:有n个学生,给出每个学生的学分和绩点,求从中去掉k个学生后剩下学生的“总绩点”的最大值。


分析: 01分数规划裸题。设“总绩点”最大值为x,s[i]c[i]s[i]=x∑s[i]∗c[i]∑s[i]=x(s[i]c[i]xc[i])=0∑(s[i]∗c[i]−x∗c[i])=0。因为[0-x)间一定有(s[i]c[i]xc[i])>0∑(s[i]∗c[i]−x∗c[i])>0,(x, max_right]间一定有(s[i]c[i]xc[i])<0∑(s[i]∗c[i]−x∗c[i])<0(具体证明暂时不会)。故可二分x得到答案。复杂度为O(nlog2nlog2(10001e6)nlog2n∗log2(1000∗1e6))。


代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5;
int n, k;
struct fct{int n, d;} s[N];
double val;
bool cmp(fct a, fct b)
{
    return a.n - a.d * val > b.n - b.d * val;
}
int main()
{
    cin >> n >> k;
    k = n - k;
    for (int i = 0; i < n; i++)
        cin >> s[i].d;
    for (int i = 0; i < n; i++)
    {
        cin >> s[i].n;
        s[i].n *= s[i].d;
    }
    double l = 0, r = 1000;
    while (r - l > 1e-6)
    {
        val = (l + r) / 2;
        sort(s, s + n, cmp);
        double ans = 0;
        for (int i = 0; i < k; i++)
            ans += s[i].n - val * s[i].d;
        if (ans >= 0)
            l = val;
        else
            r = val;
    }
    cout << setprecision(5) << fixed << val << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值